【问题标题】:PDFBox insert images inline with TEXTPDFBox 插入与 TEXT 内联的图像
【发布时间】:2017-10-20 11:33:02
【问题描述】:

我是第一次使用 PDFBox 来生成 PDF。我有一个文本文档,其中包含由我的 java 程序生成的一系列约 40 个多项选择题。一些问题与需要插入问题上方的小图像相关联。 出于这个原因,我将文本文档转换为 PDF 并希望在其上插入图像。

我已设法将图像插入 PDF 文档,但它像背景一样覆盖文本。 我想将图像与文本对齐(如 word 格式文本框,内联)。 似乎插入图像类需要一个绝对位置,这取决于文本的位置。

我怎么知道在哪里绘制我的图像?

了解 PDFBox 2.0.7.jar

import ExamDatabase.ReadInputFile;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.PDFontFactory;//???look up
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.pdmodel.font.PDSimpleFont;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.image.PDInlineImage;

/**
 *
 * @author Steve carr
 */
public class HelloWorldPdf1_1_1
{
    //runs

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException
    {

        ReadInputFile fileI = new ReadInputFile();// read plain text file text file
        ArrayList<String> localList = fileI.readerNew();

        // Create a document and add a page to it
        try (PDDocument document = new PDDocument())
        {
            PDPage page = new PDPage();
            document.addPage(page);

            // Create a new font1 object selecting one of the PDF base fonts
            PDFont font1 = PDType1Font.HELVETICA;//TIMES_ROMAN;
            PDFont font2 = PDType1Font.TIMES_ROMAN;
            PDFont font3 = PDType1Font.COURIER_BOLD;

            try (PDPageContentStream contentStream = new PDPageContentStream(document, page))
            {

                //Creating PDImageXObject object
                PDImageXObject pdImage = PDImageXObject.createFromFile("C:/PdfBox_Examples/CARD00.GIF", document);

                //**creating the PDPageContentStream object
                //PDPageContentStream contents = new PDPageContentStream(document, page);
                //**Drawing the image in the PDF document           
                contentStream.drawImage(pdImage, 100, 500, 50, 70);//1ST number is horizontal posn from left

                //****TEXTTEXTTEXTTEXT
                // Define a text content stream using the selected font1, moving the cursor and drawing the text "Hello World"
                contentStream.beginText();

                contentStream.setFont(font1, 11);

                contentStream.newLineAtOffset(0, 0);
                contentStream.setCharacterSpacing(0);
                contentStream.setWordSpacing(0);
                contentStream.setLeading(0);
                contentStream.setLeading(14.5f);// this was key for some reason

                contentStream.moveTextPositionByAmount(100, 700);// sets the start point of text

                System.out.println("localList.size= " + localList.size());//just checking within bounds during testing

                String line;
                int i;

                for (i = 0; i < 138; ++i)
                {
                    System.out.println(localList.get(i));
                    line = localList.get(i);

                    contentStream.drawString(line);
                    contentStream.newLine();
                }

                contentStream.endText();
                //******************************************************
                // Make sure that the content stream is closed:
                contentStream.close();
            }

            // Save the results and ensure that the document is properly closed:
            document.save("Hello World.pdf");
        }
    }
}

在图像顶部显示文本的结果输出:

【问题讨论】:

  • 稍微改进了格式
  • 您似乎尝试使用其他帐户添加代码。请使用您创建问题的帐户和edit问题本身添加代码。
  • 调用contentStream.newLine();时,前导值设置垂直移动。因此,您应该做的是在绘制图像时使用适当的值。因此,只需从初始值(例如 700)中减去前导(14.5)所需的时间,并将其用于 drawImage。
  • “需要在问题上方插入的小图片”“我想将图片与文字对齐”.. .这些句子似乎矛盾,还是我误解了“内联”这个词?
  • 我又读了一遍全文...恕我直言,唯一的问题似乎是您无法决定将图像放在哪里。您需要自己进行这些计算,即没有可以询问“我目前在哪里”的“光标”。

标签: pdf pdfbox


【解决方案1】:

根据此 pdf 框修复:https://issues.apache.org/jira/browse/PDFBOX-738,仅当设置 rgba 时才会保留透明度。因此,如果保留透明度,它将看起来与其他文本内联而不是覆盖,因此这可能是您的解决方案问题的第一部分,即覆盖问题。

这个例子可以帮助你找到如何计算特定文本占用的宽度,从而计算在文本之后放置图像的位置: https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/interactive/form/DetermineTextFitsField.java?revision=1749360&view=markup

【讨论】:

  • 我的输出应该是这样的
  • 我的 pdf 应该有:- /a small image/new line then begin text and end text/new line /a small image/new line then begin text and end text/newline /a small image/换行然后开始文本并结束文本/换行/小图像/换行然后开始文本并结束文本/换行/小图像/换行然后开始文本并结束文本/换行/小图像/换行然后开始文本和结束文本/等。它是一项多项选择考试,其中一些问题的开头有小图像。可惜我不能附上例子
  • 有什么书可以帮助我,因为我发现 apache 网站对初学者没有帮助
  • 我建议您删除并重写您的问题,使其格式正确。您的 cmets 中的内容没有意义,因为 cmets 未格式化。
  • 我完成的 pdf 应该有:- [从文件加载的小图像] 1. 图像中有什么?一种。回答 a b。答案 b c.答案c [从文件加载的小图像] 2. 图像中有什么?一种。回答 a b。答案 b c.答案c [从文件加载的小图像] 3. 图像中有什么?一种。回答 a b。答案 b c.回答c
猜你喜欢
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多