java 如何在iText生成的PDF中添加图像到我的标题中?

dtcbnfnu  于 4个月前  发布在  Java
关注(0)|答案(5)|浏览(46)

我正在使用iText生成PDF。我创建了一个自定义PdfPageEventHelper来为每个页面添加页眉(和页脚)。
我的问题是我不知道如何添加图像,所以它显示在“标题框”。我只知道如何添加图像到文档内容本身(如果这是有意义的)。
这里有一些代码片段...

public static void main(String[] args) {
  Rectangle headerBox = new Rectangle(36, 54, 559, 788);
  /* ... */
  Document document = new Document(PageSize.A4, 36, 36, 154, 54);
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILENAME));
  HeaderFooter event = new HeaderFooter();
  writer.setBoxSize("headerBox", headerBox);
  writer.setPageEvent(event);
  document.open();
  addContent();
  document.close();
}

static class HeaderFooter extends PdfPageEventHelper {

  public void onEndPage(PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("headerBox");
    // add header text
    ColumnText.showTextAligned(writer.getDirectContent(),
      Element.ALIGN_RIGHT, new Phrase("Hello", fontHeader1),
      rect.getLeft(), rect.getTop(), 0);

    // add header image
    try {
      Image img = Image.getInstance("c:/mylogo.PNG");
      img.scaleToFit(100,100);
      document.add(img);
    } catch (Exception x) {
      x.printStackTrace();
    }

  }

}

字符串
任何建议的适当方式添加图像的标题非常感谢!!
罗布

lmyy7pcs

lmyy7pcs1#

我已经设置了abolute的位置和对齐的形象(在这种情况下,我把我的形象在标题)

try {
        Image img = Image.getInstance("url/logo.png");
        img.scaleToFit(100,100);  
        img.setAbsolutePosition((rect.getLeft() + rect.getRight()) / 2 - 45, rect.getTop() - 50);
        img.setAlignment(Element.ALIGN_CENTER);          
        writer.getDirectContent().addImage(img);
      } catch (Exception x) {
        x.printStackTrace();
      }

字符串
我还调整了文档的边距,以便在文档的页眉和页脚中有分隔空间。

document.setMargins(20, 20, 100, 100);

9gm1akwq

9gm1akwq2#

一个通用的解决方案,在页面顶部添加图像。我们可以通过将图像放置在顶部来实现。它可能会修复您的要求

public static void main(String[] args) throws MalformedURLException, IOException, DocumentException {
      Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("HelloWorld.pdf"));
            document.open();

            //
            // Scale the image to a certain percentage
            //
            String filename = "image.jpg";
            Image image = Image.getInstance(filename);
            image = Image.getInstance(filename);
            image.scalePercent(200f);
            image.setAbsolutePosition(0, (float) (PageSize.A4.getHeight() - 20.0));
            System.out.println(image.getScaledHeight());
            document.add(image);

            //
            // Scales the image so that it fits a certain width and
            // height
            //
            image.scaleToFit(100f, 200f);
            document.add(image);
            document.add(new Chunk("This is chunk 3. "));
            System.out.println("created");
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
}

字符串
}

p4tfgftt

p4tfgftt3#

首先,你需要将你的图像转换为base64并定义它。

string base64Image = @"your_image_base64_code";

字符串
然后我们把图像转换成字节

byte[] imageBytes = Convert.FromBase64String(base64Image);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes);


我们需要调整图像在页面上的位置

image.SetAbsolutePosition(positionX, positionY);//You must enter a float type value according to the x and y coordinates.
 image.ScaleAbsoluteHeight(40);//image height
 image.ScaleAbsoluteWidth(80); //image width           
 document.Add(image);


有关更多示例,请访问itextpdf页面

kyxcudwk

kyxcudwk4#

你犯了两个大错误。
1.您正在为每个新页面创建对象的新示例。这将导致PDF膨胀,因为图像字节将与页面一样多次添加。请在onEndPage()方法外部创建Image对象,并重复使用它。这样,图像字节将仅添加到PDF一次。
1.如文档所述,作为参数传递给onEndPage()方法的Document应被视为只读参数。禁止向其添加内容。它与您使用new Document(PageSize.A4, 36, 36, 154, 54)创建的对象不同。实际上,它是由PdfWriter示例内部创建的PdfDocument类的示例。要添加图像,你需要从writer获取PdfContentByte,然后使用addImage()添加图像。
像这样的错误可以通过阅读文档很容易地避免。你可以通过阅读我的书iText in Action保存大量的时间。

dl5txlt9

dl5txlt95#

你能试

img.setAbsolutePosition(10, 10);
writer.getDirectContent().addImage(img);

字符串
而不是

document.add(img);


onPageEnd里面?

相关问题