Open fuqiangqi opened 1 year ago
我需要使用Apache的PdfBox来对已有的pdf进行追加新的pdf页。但是新的pdf页里我需要放置的是图片,且图片的长宽不统一,现在请你作为一个java代码高手来帮我写这么一个功能,基于现有PDF页的大小追加新的pdf页来放置图片,追加多少pdf页取决于把我需要放进去的图片放完为止,往pdf里面放图片遵循按提供的图片集合顺序放,在把图片绘制到pdf页里按照图片长宽等比缩放,宽度和pdf页宽度一样,放完一张图片判断能否放下另一张图片,不能的话再新追加一个pdf页去放图片
Sure! Here is a Java code snippet that uses Apache PdfBox to append new PDF pages with images to an existing PDF. The code will iterate through the provided image collection, resizing each image to fit the width of the PDF page and adding it to the PDF. It will continue appending new pages until all images have been added. Please note that you'll need to have the PdfBox library properly set up in your project for this code to work.
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDRectangle;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.PDPage;
public class PdfAppendImage {
public static void main(String[] args) {
try {
// Load the existing PDF
PDDocument document = PDDocument.load(new File("existing.pdf"));
// Get the first page of the existing PDF
PDPage existingPage = document.getPage(0);
PDRectangle existingPageRect = existingPage.getMediaBox();
// Load the images to be appended
List<String> imagePaths = List.of("image1.jpg", "image2.jpg", "image3.jpg");
// Iterate through the images and append them to the PDF
for (String imagePath : imagePaths) {
PDImageXObject image = PDImageXObject.createFromFile(imagePath, document);
// Adjust image size to fit the width of the PDF page
float pageWidth = existingPageRect.getWidth();
float scaleFactor = pageWidth / image.getWidth();
float scaledHeight = image.getHeight() * scaleFactor;
// Create a new page with the same size as the existing page
PDPage newPage = new PDPage(existingPageRect);
COSDictionary newPageDict = newPage.getCOSObject();
newPageDict.setItem(COSName.RESOURCES, existingPage.getResources());
newPageDict.setItem(COSName.MEDIA_BOX, existingPageRect);
// Add the image to the new page
PDStream newPageContents = new PDStream(document);
newPage.setContents(newPageContents);
try (PDPageContentStream contentStream = new PDPageContentStream(document, newPageContents)) {
contentStream.drawImage(LosslessFactory.createFromImage(document, image), 0, 0, pageWidth, scaledHeight);
}
// Add the new page to the document
document.addPage(newPage);
// Check if the new image can fit on the existing page, if not, create a new page
if (scaledHeight + existingPageRect.getLowerLeftY() > existingPageRect.getUpperRightY()) {
existingPage = newPage;
existingPageRect = existingPage.getMediaBox();
}
}
// Save the modified PDF
document.save(new File("modified.pdf"));
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Please make sure to replace "existing.pdf" with the path to your existing PDF file, and provide the correct image paths in the imagePaths
list. The images should be in a format supported by PdfBox, such as JPEG or PNG.
Let me know if you have any further questions!
请基于pdfbox 2.0.23版本完成
Certainly! Here's an updated version of the code snippet that is compatible with PdfBox 2.0.23:
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.examples.signature.CreateEmptySignatureForm;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDRectangle;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
public class PdfAppendImage {
public static void main(String[] args) {
try {
// Load the existing PDF
PDDocument document = PDDocument.load(new File("existing.pdf"));
// Get the first page of the existing PDF
PDPage existingPage = document.getPage(0);
PDRectangle existingPageRect = existingPage.getMediaBox();
// Load the images to be appended
List<String> imagePaths = List.of("image1.jpg", "image2.jpg", "image3.jpg");
// Iterate through the images and append them to the PDF
for (String imagePath : imagePaths) {
PDImageXObject image = PDImageXObject.createFromImage(document, imagePath);
// Adjust image size to fit the width of the PDF page
float pageWidth = existingPageRect.getWidth();
float scaleFactor = pageWidth / image.getWidth();
float scaledHeight = image.getHeight() * scaleFactor;
// Create a new page with the same size as the existing page
PDPage newPage = new PDPage(existingPageRect);
COSDictionary newPageDict = newPage.getCOSObject();
newPageDict.setItem(COSName.RESOURCES, existingPage.getResources());
newPageDict.setItem(COSName.MEDIA_BOX, existingPageRect);
// Add the image to the new page
PDStream newPageContents = new PDStream(document);
newPage.setContents(newPageContents);
try (PDPageContentStream contentStream = new PDPageContentStream(document, newPageContents)) {
contentStream.drawImage(image, 0, 0, pageWidth, scaledHeight);
}
// Add the new page to the document
document.addPage(newPage);
// Check if the new image can fit on the existing page, if not, create a new page
if (scaledHeight + existingPageRect.getLowerLeftY() > existingPageRect.getUpperRightY()) {
existingPage = newPage;
existingPageRect = existingPage.getMediaBox();
}
}
// Save the modified PDF
document.save(new File("modified.pdf"));
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Ensure that you replace "existing.pdf" with the correct path to your existing PDF file and provide the correct image paths in the imagePaths
list. The images should be in a format supported by PdfBox, such as JPEG or PNG.
Let me know if you have any further questions!
分析这段docker命令 为什么镜像会启动失败 并且docker ps 看不到容器 sudo nvidia-docker run --rm --net=host -v /usr/local/recognize:/recognize_document/log -d recognize_general_receipt_20380:gpu -p 10000 -I -G 0 -L 127.0.0.1
该Docker命令解析如下:
sudo nvidia-docker run --rm --net=host -v /usr/local/recognize:/recognize_document/log -d recognize_general_receipt_20380:gpu -p 10000 -I -G 0 -L 127.0.0.1
sudo
:以超级用户权限运行Docker命令。nvidia-docker
:用于在NVIDIA GPU上运行Docker容器。run
:运行容器命令。--rm
:容器停止后立即删除该容器。--net=host
:将主机网络命名空间共享给容器,使容器能够访问主机的网络配置。-v /usr/local/recognize:/recognize_document/log
:将主机目录/usr/local/recognize
挂载到容器的/recognize_document/log
目录下,用于共享数据或日志文件。-d
:以后台守护进程方式运行容器。recognize_general_receipt_20380:gpu
:镜像名称和标签。-p 10000
:将容器的端口10000映射到主机的一个随机端口。-I -G 0 -L 127.0.0.1
:传递给容器的其他命令行参数。关于为什么镜像启动失败以及为什么docker ps
命令无法看到容器,可能存在以下可能原因:
nvidia-docker
命令:确保已正确安装并配置了nvidia-docker
。该命令用于在支持NVIDIA GPU的主机上运行容器。recognize_general_receipt_20380:gpu
镜像名称和标签是否存在,并正确配置了GPU支持。docker logs <container-id>
来查看容器的日志输出,以便定位问题。docker ps
命令看不到容器,容器可能已启动但已经停止。您可以运行命令docker ps -a
来查看所有容器的状态,包括已停止的容器。尝试根据上述可能原因检查并解决相关问题,以便启动和查看容器的状态。
使用vantUI 与vue 画一个移动端界面,界面要求可以适应多种分辨率的屏幕,第一屏界面有三个大块,大块颜色为白底,每个块之间都有边距,和屏幕左右也有边距,这些边距相等,第一个块左上角有一个标题,然后下方有内容 标题,报销人 标题居左,标题内容居于 呈两端对齐,下面依次是报销人居左 李白文字居右,下面两个块都是白块,没内容,三个白块均分可用显示空间,提供一个这么一个vue文件给我
当使用Vant UI和Vue来创建一个适应多种分辨率的移动端界面时,可以按照以下步骤进行操作:
安装Vue和Vant UI:
npm install vue
npm install vant
创建一个Vue组件,并在组件中引入Vant UI和样式:
<template>
<div class="container">
<div class="block">
<div class="title">标题</div>
<div class="content">报销人</div>
<div class="content">李白</div>
</div>
<div class="block"></div>
<div class="block"></div>
</div>
</template>
<script>
import 'vant/lib/index.css'; // 引入Vant UI的样式
export default {
name: 'MobileScreen',
};
</script>
<style scoped>
.container {
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
.block {
width: calc(100% - 40px);
height: calc(33.33% - 20px);
margin: 10px;
background-color: #fff;
}
.title {
text-align: left;
font-weight: bold;
}
.content {
text-align: justify;
}
</style>
在上面的示例中,使用了Vant UI中的样式,container
样式用于包裹所有块,并设置了内边距和居中显示。block
样式设置了块的样式和边距,并将背景颜色设置为白色。title
样式用于设置标题的样式,content
样式用于设置内容(报销人和李白)的样式。
在需要使用该组件的地方引入并使用它:
<template>
<div>
<!-- 其他内容 -->
<mobile-screen></mobile-screen>
<!-- 其他内容 -->
</div>
</template>
<script>
import MobileScreen from '@/components/MobileScreen.vue'; // 导入移动端界面的组件
export default {
components: {
MobileScreen,
},
};
</script>
在上述示例中,将移动端界面的组件MobileScreen
导入并在需要使用的地方进行注册和使用。
这样就完成了一个基于Vant UI和Vue的移动端界面,可以自适应多种分辨率的屏幕,并满足您的要求。您可以根据需要进一步定制样式和添加内容。
请注意,上述示例中使用了简化的布局和样式,您可以根据实际需求进行调整和扩展。
Hello, I am your assistant on GitHub, built by the Second State team. I am ready for your question now! How can I help you with your work on GitHub?