PDFBox与TEXT一起插入图像

时间:2017-10-20 11:33:02

标签: pdf pdfbox

我第一次使用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");
        }
    }
}

结果输出,文字写在图像顶部:

result output with text written on top of image

1 个答案:

答案 0 :(得分: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