使用带有页眉和页脚的Itext将使用apache蜡染从SVG生成的外部pdf内容添加到源PDF

时间:2019-06-03 10:33:03

标签: pdf svg itext itext7 batik

我有以下要求。

  • 使用Apache蜡染将SVG转换为PDF
  • 使用IText 7使用页眉和页脚准备源PDF文档
  • 获取转换后的PDF并将其嵌入到源PDF的内容中

我已经看到IText支持将SVG转换为Image,但是输出不正确。蜡染的输出似乎是完美的。

下面是我的代码。任何人都可以提出适当的方法吗?我无法实现

使用蜡染将SVG转换为PDF

FileInputStream inputStream = new FileInputStream(new File(Paths.get("Input").toAbsolutePath()+"/test.svg"));
               byte[] bytes = IOUtils.toByteArray(inputStream);
               FileOutputStream pdfOutputStream = new FileOutputStream(new File(Paths.get("Output").toAbsolutePath()+"/convertedSvg.pdf"));
               Transcoder transcoder = new PDFTranscoder();
               TranscoderInput transcoderInput = new TranscoderInput(new ByteArrayInputStream(bytes));
               TranscoderOutput transcoderOutput = new TranscoderOutput(pdfOutputStream);
               int dpi = 300;
               transcoder.addTranscodingHint(PDFTranscoder.KEY_WIDTH, new Float(dpi * 29.7));
               transcoder.addTranscodingHint(PDFTranscoder.KEY_HEIGHT, new Float(dpi * 42.0));
               transcoder.addTranscodingHint(PDFTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER,(25.4f / 72f));
               transcoder.transcode(transcoderInput, transcoderOutput);

iText代码

PdfWriter writer = new PdfWriter(new FileOutputStream(new File(Paths.get("Output").toAbsolutePath()+"/final.pdf")));
PdfDocument pdfDoc = new PdfDocument(writer);    
pdfDoc.setDefaultPageSize(PageSize.A3.rotate());
NormalPageHeader headerHandler = new NormalPageHeader(Paths.get("images").toAbsolutePath() + "\\logo.png", pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
PageEndEvent pageEndEvent = new PageEndEvent(Paths.get("images").toAbsolutePath() + "\\FooterLineExternal.png" ,pdfFontMap);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, pageEndEvent);
Document doc = new Document(pdfDoc);
doc.getPageEffectiveArea(PageSize.A3.rotate());
Table imageTable = new Table(1);
imageTable.setBorder(Border.NO_BORDER);
imageTable.setWidth(UnitValue.createPercentValue(100));
Cell cell = new Cell();
Paragraph paragraph = new Paragraph("Horizontal Trajectory");
paragraph.setVerticalAlignment(VerticalAlignment.TOP);
cell.add(paragraph);
cell.setBorder(Border.NO_BORDER);
cell.setPaddingTop(50);
imageTable.addCell(cell);
doc.add(imageTable);
doc.close();

0 个答案:

没有答案