【问题标题】:Programmatically create PDF photo albums以编程方式创建 PDF 相册
【发布时间】:2010-11-24 13:30:57
【问题描述】:

我有一套 PDF 相册模板(它们有空白方块,应该放在文本和照片上)。我的需要是使用这些模板来生成实际的专辑。

我的计划是使用 iText 和 Java。我会向应用程序发送一个包含所有要使用的图像 URL 的数组。我会确切地知道图像应该放在模板上的什么位置,并使用绝对定位来放置它们。

我只是想知道是否有更简单或更简洁的方法来执行此操作 - 即使这意味着使用不同的语言?生成 PDF 后,它们会自动发送到要求它们为 PDF 的打印机。

【问题讨论】:

    标签: java pdf pdf-generation


    【解决方案1】:

    我认为这可以使用ImageMagick 以多种语言(甚至是shell 脚本)相当容易地完成。

    【讨论】:

      【解决方案2】:

      尚不清楚您是否有文字环绕照片。无论如何,我会看看Apache-FOP。我已经使用了很多 - 它可能需要一些自定义。

      【讨论】:

        【解决方案3】:
        PdfReader reader = new PdfReader("template.pdf");
        PdfStamper stamper = new PdfStamper( reader, new FileOutputStream("output.pdf"));
        
        PdfContentByte content = stamper.getOverContent(1); // first page == 1
        
        Image image = Image.createInstance("someImage.png");
        image.setAbsolutePosition( x, y );
        image.setAbsoluteHeight( hei );
        image.setAbsoluteWidth( wid );
        
        content.addImage(image);
        
        // instead of absolutely positioning the image, you can do:
        //content.addImage( image, wid, 0f, 0f, hei, x, y ); 
        // that's a transformation matrix, you can skew, rotate, scale, etc.
        
        stamper.close();
        

        如果您不知道确切的位置,您有几个选择。

        1) 替换现有图像。

        使用此选项,您实际上可以让模板稍微突出图像,使用 Alpha 混合等。获得幻想。如果您使用stamper.getUnderContent(1) 并使用透明背景构建模板,则可以对上述代码执行相同的操作。我刚刚回答了一个replace an image question,应该会在这方面有所帮助。

        新图像将继承占位符图像的所有图形状态。 x,y,wid,hei, 可选内容组, 透明度, 各种东西。

        2)在 Acrobat 中(或者我想是 iText)中为模板添加注释(字段易于访问),并使用它的 RECT 为上述代码提供 x,y,wid,hei。

        // given the above stamper
        AcroFields fields = stamper.getAcroFields();
        PdfDictionary replaceAnnot = fields.getFieldItem("ReplaceMe").getMerged(0);
        
        //we can remove the field from the PDF without breaking the info in replaceAnnot.
        fields.removeField("ReplaceMe");
        
        // rects are laid out [llx, lly, urx, ury]
        PdfArray rect = replaceAnnot.getAsArray(PdfName.RECT);
        float x = rect.getAsNumber(0).floatValue();
        float y = rect.getAsNumber(1).floatValue();
        float width = rect.getAsNumber(2).floatValue() - x;
        float height = rect.getAsNumber(3).floatValue() - y;
        

        将这些坐标输入到上面的图像代码中,你就可以开始了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-24
          • 1970-01-01
          • 2012-09-10
          • 2010-12-21
          • 2011-12-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多