Drawing vector images in PDF using PDFBox

I would like to draw a vector image in a PDF file using Apache PDFBox.

This is the code I use to draw regular images

PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(1); PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true); BufferedImage _prevImage = ImageIO.read(new FileInputStream("path/to/image.png")); PDPixelMap prevImage = new PDPixelMap(document, _prevImage); contentStream.drawXObject(prevImage, prevX, prevY, imageWidth, imageHeight); 

If you use svg or wmf image instead of png, then the resulting PDF document will be damaged.

The main reason I want the image to be a vector image is because it looks awful with PNG or JPG, I think it is somehow compressed, so it looks bad. This should not happen with vector images (well, when I export SVG paths as PDF to Inkscape, this does not happen, vector paths are saved).

Is there a way to draw svg or wmf (or another vector) in a PDF using Apache PDFBox?

I am currently using PDFBox 1.8, if that matters.

+5
source share
1 answer

I do this, but not directly. First convert your SVG documents to PDF documents using FOP librairy and Batik. https://xmlgraphics.apache.org/fop/dev/design/svg.html .

For the second time, you can use LayerUtility in pdfbox to convert your new pdf document to PDXObjectForm. After that, you just need to include PDXObjectForm in your final pdf documents.

0
source

All Articles