gpt4 book ai didi

java - 如何使用 OpenPDF 添加段落和边框

转载 作者:行者123 更新时间:2023-12-02 01:21:31 28 4
gpt4 key购买 nike

我正在使用 OpenPDF 生成 PDF 文档,并将页眉和页脚图像添加到 PDF 中。需要将一个段落添加到 PDF 的中心,其中一些文本内容是基于输入请求的动态内容,并且所有内容都需要用边框包围。我能够生成添加了页眉和页脚的 PDF,但在将段落添加到 pdf 中心和边框时遇到问题。如有任何帮助,我们将不胜感激。

我尝试了不同的方法,通过添加表格或使用 Canvas ,但无法解决。

   public void generatePDF(String firstName, String lastName){
try {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(DEST));
//setting font family, color
Font font = new Font(Font.HELVETICA, 16, Font.BOLDITALIC, Color.RED);
doc.open();
addHeaderImageToPDF(doc,writer);
Paragraph para = new Paragraph("Hello! This PDF is created for "+firstName+" "+lastName, font);
para.setAlignment(Element.ALIGN_MIDDLE);
doc.add(para);
addFooterImageToPDF(doc,writer);
addBorderToPDF(doc);
doc.close();
writer.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void addBorderToPDF(Document document){
Rectangle rect= new Rectangle(531.5f,820,38.5f,0); // you can resize rectangle
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorderColor(Color.BLACK);
rect.setBorderWidth(2);
document.add(rect);
}

private void addHeaderImageToPDF(Document document, PdfWriter writer){
try {
Image image = Image.getInstance(HEADER_IMAGE_PATH);
//image = Image.getInstance(IMAGE_PATH);
image.scalePercent(50f);
//image.setAbsolutePosition(0, (float) (PageSize.A4.getHeight() - 20.0));
image.setAbsolutePosition(40, 675);
System.out.println(image.getScaledHeight());
document.add(image);
image.scaleToFit(100f, 100f);
//document.add(image);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private void addFooterImageToPDF(Document document, PdfWriter writer){
try {
Image image = Image.getInstance(FOOTER_IMAGE_PATH);
//image = Image.getInstance(IMAGE_PATH);
image.scalePercent(50f);
//image.setAbsolutePosition(0, (float) (PageSize.A4.getHeight() - 20.0));
image.setAbsolutePosition(40, 450);
System.out.println(image.getScaledHeight());
document.add(image);
image.scaleToFit(100f, 100f);
//document.add(image);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

想要在中心添加段落,并且 PDF 的整个内容应被深蓝色边框包围。如有任何帮助,我们将不胜感激。

最佳答案

作为给定的标签 , ,和意味着可以自由选择 PDF 库,这里是使用 iText 7 (v7.1.7) 创建带有深蓝色边框的居中段落的示例:

带边框的居中段落

String firstName = "Mister";
String lastName = "Nine";
try ( PdfWriter pdfWriter = new PdfWriter(new File("CenterParagraph.pdf"));
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument) ) {
Paragraph paragraph = new Paragraph("Hello! This PDF is created for "+firstName+" "+lastName);
paragraph.setWidth(100).setBorder(new SolidBorder(new DeviceRgb(0f, 0f, 0.6f), 3));
PageSize box = pdfDocument.getDefaultPageSize();
document.showTextAligned(paragraph, (box.getLeft() + box.getRight()) / 2, (box.getTop() + box.getBottom()) / 2,
TextAlignment.CENTER, VerticalAlignment.MIDDLE);
}

( ShowTextAtPosition 测试 testShowCenteredBorderedParagraph)

结果

screen shot

在页面中间。

带有页眉和页脚图像以及页面边框的居中段落

String firstName = "Mister";
String lastName = "Nine";

Image img = null;
try ( InputStream imageResource = IMAGE_INPUT_STREAM ) {
ImageData data = ImageDataFactory.create(StreamUtil.inputStreamToArray(imageResource));
img = new Image(data);
img.scaleToFit(100f, 100f);
}

try ( PdfWriter pdfWriter = new PdfWriter(new File("CenterParagraphWithExtras.pdf"));
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument) ) {
PageSize box = pdfDocument.getDefaultPageSize();

Paragraph paragraph = new Paragraph("Hello! This PDF is created for "+firstName+" "+lastName);
paragraph.setWidth(100);
document.showTextAligned(paragraph, (box.getLeft() + box.getRight()) / 2, (box.getTop() + box.getBottom()) / 2,
TextAlignment.CENTER, VerticalAlignment.MIDDLE);

PdfCanvas pdfCanvas = new PdfCanvas(pdfDocument.getLastPage());
Rectangle borderRectangle = new Rectangle(box.getLeft() + 5, box.getBottom() + 5, box.getWidth() - 10, box.getHeight() - 10);
pdfCanvas.setColor(new DeviceRgb(0f, 0f, 0.6f), false);
pdfCanvas.setLineWidth(3);
pdfCanvas.rectangle(borderRectangle);
pdfCanvas.stroke();

img.setFixedPosition(box.getLeft() + 40, box.getTop() - 150);
document.add(img);
img.setFixedPosition(box.getLeft() + 40, box.getBottom() + 50);
document.add(img);
}

结果

screen shot

关于java - 如何使用 OpenPDF 添加段落和边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57602156/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com