質問

iTextを使用してJavaからPDFを生成します(このサイトの推奨事項に一部基づいています)。ただし、GIFのような画像形式でロゴのコピーを埋め込むと、人々がズームインおよびズームアウトすると少し奇妙に見えます。

理想的には、EPS、SVG、または単なるPDFテンプレートなどのベクトル形式で画像を埋め込みます。 Webサイトは、EPSサポートが廃止され、PDF内にPDFまたはPSを埋め込むとエラーが発生し、SVGについても言及していないと主張しています。

コードでは、iTextを直接使用するのではなく、Graphics2D APIを使用しますが、結果が得られた場合は、AWTモードから抜け出し、iText自体を使用します。これはどのように行うことができますか?

役に立ちましたか?

解決

ドキュメント iTextは次の画像形式をサポートしています。 、GIF、PNG、TIFF、BMP、WMF、EPS。これが助けになるかどうかはわかりませんが、 iTextSharp を使用してベクトル pdfファイルのWMF 画像:

C#:

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

public class Program 
{

    public static void Main() 
    {
        Document document = new Document();
        using (Stream outputPdfStream = new FileStream("output.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        using (Stream imageStream = new FileStream("test.wmf", FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            PdfWriter.GetInstance(document, outputPdfStream);
            Image wmf = Image.GetInstance(imageStream);
            document.Open();
            document.Add(wmf);
            document.Close();
        }
    }
}

他のヒント

これは、ここで見つけた投稿と公式の例から得たものです:

/**
 * Reads an SVG Image file into an com.itextpdf.text.Image instance to embed it into a PDF
 * @param svgPath SVG filepath
 * @param writer PdfWriter instance 
 * @return Instance of com.itextpdf.text.Image holding the SVG file
 * @throws IOException
 * @throws BadElementException
 */
private static Image getSVGImage(String svgPath, PdfWriter writer) throws IOException, BadElementException {
    SVGDocument svgDoc = new SAXSVGDocumentFactory(null).createSVGDocument(null, new FileReader(svgPath));

    // Try to read embedded height and width
    float svgWidth = Float.parseFloat(svgDoc.getDocumentElement().getAttribute("width").replaceAll("[^0-9.,]",""));
    float svgHeight = Float.parseFloat(svgDoc.getDocumentElement().getAttribute("height").replaceAll("[^0-9.,]",""));

    PdfTemplate svgTempl = PdfTemplate.createTemplate(writer, svgWidth, svgHeight);
    Graphics2D g2d = new PdfGraphics2D(svgTempl, svgTempl.getWidth(), svgTempl.getHeight());
    GraphicsNode chartGfx = (new GVTBuilder()).build(new BridgeContext(new UserAgentAdapter()), svgDoc);
    chartGfx.paint(g2d);
    g2d.dispose();

    return new ImgTemplate(svgTempl);
}

Imageインスタンスは、pdfに簡単に追加できます(私の場合、署名として)。

Graphics2D APIとApache Batikライブラリを使用してPDFにSVGを描画するiText作成者によるいくつかの例を見つけました。

http://itextpdf.com/examples/iia.php?id=269

http://itextpdf.com/examples/iia.php?id=263

目的のために、SVGの文字列を取得し、それを特定のサイズと位置でPDFに描画する一方で、画像のベクトルの性質を維持する必要がありました(ラスタライズなし)。

SAXSVGDocumentFactory.createSVGDocument()関数でよく見られるSVGファイルをバイパスしたかった。フラットファイルの代わりにSVGテキスト文字列を使用すると、次の投稿が役立つことがわかりました。

http://batik.2283329.n4。 nabble.com/Parse-SVG-from-String-td3539080.html

  

文字列からStringReaderを作成し、それをSAXSVGDocumentFactory#createDocument(String、Reader)メソッドに渡す必要があります。文字列として最初のパラメーターとして渡すURIは、SVGドキュメントのベースドキュメントURIになります。これは、SVGが外部ファイルを参照する場合にのみ重要です。

     

よろしく、

     

ダニエル

iTextの例から派生したJavaソース:

// SVG as a text string.
String svg = "<svg>...</svg>";

// Create the PDF document.
// rootPath is the present working directory path.
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(rootPath + "svg.pdf")));
document.open();

// Add paragraphs to the document...
document.add(new Paragraph("Paragraph 1"));
document.add(new Paragraph(" "));

// Boilerplate for drawing the SVG to the PDF.
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
PdfContentByte cb = writer.getDirectContent();

// Parse the SVG and draw it to the PDF.
Graphics2D g2d = new PdfGraphics2D(cb, 725, 400);
SVGDocument chart = factory.createSVGDocument(rootPath, new StringReader(svg));
GraphicsNode chartGfx = builder.build(ctx, chart);
chartGfx.paint(g2d);
g2d.dispose();

// Add paragraphs to the document...
document.add(new Paragraph("Paragraph 2"));
document.add(new Paragraph(" "));

document.close();

これにより、作業中のPDFにSVGが描画されることに注意してください。 SVGはテキストの上にフローティングレイヤーとして表示されます。私はまだそれを移動/スケーリングし、テキストとインラインに配置することに取り組んでいますが、それが問題の直接の範囲外であることを願っています。

お役に立てば幸いです。

乾杯

編集:次を使用して、svgをインラインオブジェクトとして実装できました。コメント付きの行は、位置を確認するためのクイックボーダーを追加するためのものです。

SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
SVGDocument svgDoc = factory.createSVGDocument(rootPath, new StringReader(svg));
PdfTemplate svgTempl = PdfTemplate.createTemplate(writer, Float.parseFloat(svgDoc.getDocumentElement().getAttribute("width")), Float.parseFloat(svgDoc.getDocumentElement().getAttribute("height")));
Graphics2D g2d = new PdfGraphics2D(svgTempl, svgTempl.getWidth(), svgTempl.getHeight());
GraphicsNode chartGfx = builder.build(ctx, svgDoc);
chartGfx.paint(g2d);
g2d.dispose();
Image svgImg = new ImgTemplate(svgTempl);
svgImg.setAlignment(Image.ALIGN_CENTER);
//svgImg.setBorder(Image.BOX);
//svgImg.setBorderColor(new BaseColor(0xff, 0x00, 0x00));
//svgImg.setBorderWidth(1);
document.add(svgImg);

最近、Graphics2Dオブジェクトを直接iTextに送信できることを学びました。結果のPDFファイルは、スケーラブルなベクターグラフィックスと同じくらい優れています。あなたの投稿から、これはあなたの問題を解決するかもしれないと思われます。

Document document = new Document(PageSize.LETTER);
PdfWriter writer = null;
try {
    writer = PdfWriter.getInstance(document, new FileOutputStream(your file name));
} catch (Exception e) {
    // do something with exception
}

document.open();

PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper());

// Create your graphics here - draw on the g2 Graphics object

g2.dispose();
cb.addTemplate(tp, 0, 100); // 0, 100 = x,y positioning of graphics in PDF page
document.close();

これは、itext 7.1.3を使用して、SVGConverterによってSVG画像をレンダリングするのに役立ちました。

PdfWriter writer = new PdfWriter(new FileOutputStream("/home/users/Documents/pdf/new.pdf"));

        PdfDocument pdfDoc = new PdfDocument(writer);

        Document doc = new Document(pdfDoc);

        URL svgUrl = new File(svg).toURI().toURL();

        doc.add(new Paragraph("new pikachu"));                      

        Image image = SvgConverter.convertToImage(svgUrl.openStream(), pdfDoc);                 
        doc.add(image);

        doc.close();

iTextの新しいバージョンはSVGファイルもサポートしています。 このページを参照してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top