有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在ITextpdf中的图像上写入文本?

我正在用Java开发iTextPdf

我想要文字在图像上的绝对位置。像这样:

enter image description here

在教程中,我尝试使用showTextAlignedIn DrawContext来布局文本,但我无法控制字体

请告诉我,你如何能有多个文本在所需的位置与所需的字体颜色和大小的图像


共 (2) 个答案

  1. # 1 楼答案

    您可以在生成的pdf上使用iText PDFstamper
    有关更多详细信息,请参阅以下内容:
    Changing the font color and size when using FontSelector https://developers.itextpdf.com/question/how-add-watermark-page-opaque-image

    import java.io.FileOutputStream;
    
    import com.itextpdf.text.Element;
    import com.itextpdf.text.Phrase;
    import com.itextpdf.text.pdf.ColumnText;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfStamper;
    
    
    class PDFStamperExample
    {
        public static void main(String[] args)
        {
            try
            {
                PdfReader pdfReader = new PdfReader("HelloWorld.pdf");
                PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("HelloWorldModified.pdf"));
                PdfContentByte canvas = pdfStamper.getOverContent(1);
                ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 250, 750, 0);
                pdfStamper.close();
                pdfReader.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
  2. # 2 楼答案

    首先,您可以尝试将图像序列设置为true,如下所示:

    writer.setStrictImageSequence(true);
    

    另外,我们可以看看你写的一些代码来看看问题吗? 干杯