我们使用iText从Java生成PDF(部分基于本网站的推荐)。但是,以GIF等图像格式嵌入我们的徽标副本会导致人们放大和缩小时看起来有点奇怪。

理想情况下,我们希望将图像嵌入到矢量格式中,例如EPS,SVG或仅仅是PDF模板。该网站声称已经放弃了EPS支持,在PDF中嵌入PDF或PS可能会导致错误,甚至没有提到SVG。

我们的代码直接使用Graphics2D API而不是iText,但是如果达到了结果,我们愿意打破AWT模式并使用iText本身。怎么办呢?

有帮助吗?

解决方案

根据文档,iText支持以下图像格式:JPEG ,GIF,PNG,TIFF,BMP,WMF和EPS。我不知道这是否有任何帮助,但我已成功使用 iTextSharp 嵌入向量 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中(在我的情况下作为签名)。

我发现了iText作者的几个例子,它们使用Graphics2D API和Apache Batik库在PDF中绘制SVG。

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

  

您必须从String创建StringReader并将其传递给SAXSVGDocumentFactory#createDocument(String,Reader)方法。作为String传递的第一个参数的URI将是SVG文档的基本文档URI。只有在SVG引用任何外部文件时,这才应该很重要。

     

致以最诚挚的问候,

     

丹尼尔

源自iText示例的Java Source:

// 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();

请注意,这会将SVG绘制到您正在处理的PDF中。 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