本文介绍了Apache PDFBox:如何指定我输出的文本的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我在特定位置生成pdf时已经实现了创建文本.

但现在我的问题是,我该如何处理位于不同位置的文本

预期生成的 PDF:

 contentStream.setLeading(15);contentStream.newLineAtOffset(175, 670);String text = "文本 1";String text1 = "文本 2";String text2 = "文本 3";String text3 = "文本 4";String text4 = "文本 5";contentStream.showText(文本);contentStream.newLine();contentStream.showText(text1);contentStream.newLine();contentStream.showText(text2);contentStream.newLine();contentStream.showText(text3);contentStream.newLine();contentStream.showText(text4);contentStream.newLine();contentStream.showText(text5);
解决方案

在你的代码中,你已经选择了第一行开始的位置,如下所示:

contentStream.newLineAtOffset(175, 670);

关于你的问题

如何处理位于不同位置的文本

因此:您只需再次使用 newLineAtOffset

您必须注意,newLineAtOffset(x, y) 不会将新行的起点设置为绝对坐标x, y 而是添加这些值上一行开始的坐标,因此方法名称中的 AtOffset.

由于上一行开始坐标在文本对象的开始处重置为0, 0 (contentStream.beginText()),文本对象中的第一个 newLineAtOffset 出现使用绝对坐标.

因此,如果您更喜欢使用绝对坐标,则可以在每次需要以不同于 contentStream.newLine() 的方式移动行开始时启动一个新的文本对象.

如果您对相对坐标没问题,那么您不需要经常开始新的文本对象,而是在 newLineAtOffset 中使用从行开始到行开始的偏移量,例如:

try (PDDocument document = new PDDocument()) {PDPage 页面 = 新的 PDPage();document.addPage(page);PDFont 字体 = PDType1Font.HELVETICA;String text = "文本 1";String text1 = "文本 2";String text2 = "文本 3";String text3 = "文本 4";String text4 = "文本 5";String text5 = "文本 6";尝试(PDPageContentStream contentStream = new PDPageContentStream(文档,页面)){contentStream.beginText();contentStream.newLineAtOffset(175, 670);contentStream.setFont(font, 12);contentStream.setLeading(15);contentStream.showText(文本);contentStream.newLine();contentStream.showText(text1);contentStream.newLineAtOffset(225, 10);contentStream.setFont(font, 15);contentStream.showText(text2);contentStream.newLineAtOffset(-390, -175);contentStream.setFont(font, 13.5f);contentStream.setLeading(17);contentStream.showText(text3);contentStream.newLine();contentStream.showText(text5);contentStream.newLineAtOffset(300, 13.5f);contentStream.showText(text4);contentStream.endText();contentStream.moveTo(0, 520);contentStream.lineTo(612, 520);contentStream.stroke();}文件.保存(目标文件);}

(

反过来又近似于您的图像.(我没有计算你图片中的像素,所以这只是一个近似值.)

So basically I already achieved creating a text when generating a pdf on a specific position.

But now my problem is, how do I do it for text that are located on different positions

Expected Generated PDF:

      contentStream.setLeading(15);
      contentStream.newLineAtOffset(175, 670);

      String text = "Text 1";
      String text1 = "Text 2";
      String text2 = "Text 3";
      String text3 = "Text 4";
      String text4 = "Text 5";

      contentStream.showText(text);
      contentStream.newLine();
      contentStream.showText(text1);
      contentStream.newLine();
      contentStream.showText(text2);
      contentStream.newLine();
      contentStream.showText(text3);
      contentStream.newLine();
      contentStream.showText(text4);
      contentStream.newLine();
      contentStream.showText(text5);
解决方案

In your code you already choose the position for the first line start like this:

contentStream.newLineAtOffset(175, 670);

Concerning your question

therefore: You simply use newLineAtOffset again!

You have to be aware, though, that newLineAtOffset(x, y) does not set the new line start to the absolute coordinates x, y but instead adds these values to the coordinates of the previous line start, hence the AtOffset in the method name.

As the previous line start coordinates are reset to 0, 0 at the start of a text object (contentStream.beginText()), your first newLineAtOffset in a text object appears to use absolute coordinates.

So if you prefer to use absolute coordinates, you can start a new text object each time you need to move the line start differently than contentStream.newLine() does.

If you are ok with relative coordinates, though, you don't need to start new text objects that often but instead use offsets from line start to line start in newLineAtOffset, e.g.:

try (PDDocument document = new PDDocument()) {
    PDPage page = new PDPage();
    document.addPage(page);

    PDFont font = PDType1Font.HELVETICA;

    String text = "Text 1";
    String text1 = "Text 2";
    String text2 = "Text 3";
    String text3 = "Text 4";
    String text4 = "Text 5";
    String text5 = "Text 6";

    try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
        contentStream.beginText();

        contentStream.newLineAtOffset(175, 670);
        contentStream.setFont(font, 12);
        contentStream.setLeading(15);
        contentStream.showText(text);
        contentStream.newLine();
        contentStream.showText(text1);

        contentStream.newLineAtOffset(225, 10);
        contentStream.setFont(font, 15);
        contentStream.showText(text2);

        contentStream.newLineAtOffset(-390, -175);
        contentStream.setFont(font, 13.5f);
        contentStream.setLeading(17);
        contentStream.showText(text3);
        contentStream.newLine();
        contentStream.showText(text5);

        contentStream.newLineAtOffset(300, 13.5f);
        contentStream.showText(text4);

        contentStream.endText();

        contentStream.moveTo(0, 520);
        contentStream.lineTo(612, 520);
        contentStream.stroke();
    }

    document.save(TARGET_FILE);
}

(ArrangeText test testArrangeTextForTeamotea)

which results in

which in turn approximates your image. (I have not counted pixels in your image, so this only is an approximation.)

这篇关于Apache PDFBox:如何指定我输出的文本的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:50