简体   繁体   English

在 ITextPDF Java 中创建单个动态页面 PDF

[英]Create a single dynamic page PDF in ITextPDF Java

I have searched for many articles here about this.我在这里搜索了很多关于这个的文章。 I've found some but none that works properly for what I need.我找到了一些但没有一个适合我的需要。 So, let's get into it.所以,让我们进入它。 I am creating a file in ITextPDF which I need to print after then.我正在 ITextPDF 中创建一个文件,然后我需要打印。 And the content of the file is dynamic.并且文件的内容是动态的。 So the PDF sometimes is splitting the content in pages and when I print it, the gap between the pages is shown with a big space in the paper (I need to print in a thermal printer in a single page).因此,PDF 有时会在页面中拆分内容,当我打印它时,页面之间的间隙显示为纸张上的大空间(我需要在单页热敏打印机中打印)。 So, I spent somedays trying to find out any solutions, but yet, can't find it.所以,我花了几天时间试图找出任何解决方案,但还是找不到。 For printing I am using PDFBox, which works fine.对于打印,我使用 PDFBox,效果很好。 just need to align the PDF correctly now.现在只需要正确对齐 PDF。

PDF 示例 1

PDF 示例 2

This is a two pages PDF.这是一个两页的 PDF。 Need to make it one, remembering that this is dynamic, it may be much longer.需要让它成为一个,记住这是动态的,它可能会更长。

Code sample of how I create it:我如何创建它的代码示例:

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfDirectoryPath + 
    "temp.pdf"));
writer.setStrictImageSequence(true);
document.open();

Font bold = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 20, BaseColor.BLACK);

Chunk nomeFantasiaChunk = new Chunk(nomeFantasia, bold );
Paragraph paragrafoNomeFantasia = new Paragraph(nomeFantasiaChunk);
paragrafoNomeFantasia.setAlignment(Element.ALIGN_CENTER);
document.add(paragrafoNomeFantasia);

document.close();

Code sample of how I print it (works fine, using PDFBox):我如何打印它的代码示例(工作正常,使用 PDFBox):

private void printPDF(PrintService print, String fileName) {
    try {
        PDDocument document = PDDocument.load(new FileInputStream(Constants.pdf + fileName));
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(new PDFPageable(document));

        // define custom paper
        Paper paper = new Paper();
        paper.setSize(225, 396); // 1/72 inch
        paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins

        // custom page format
        PageFormat pageFormat = new PageFormat();
        pageFormat.setPaper(paper);

        // override the page format
        Book book = new Book();
        // append all pages
        book.append(new PDFPrintable(document), pageFormat, document.getNumberOfPages());
        job.setPageable(book);
        job.setPrintService(print);
        job.print();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

It is all fine, just need to set that PDF into one page dynamically.一切都很好,只需要将该PDF动态设置为一页即可。 I have found some articles that came a bit closer, but never to a real match solution.我发现了一些更接近的文章,但从来没有找到真正的匹配解决方案。

This one, for example: Create new single-page-PDF document from one page of a multipage PDF document这个,例如: 从多页 PDF 文档的一页创建新的单页 PDF 文档

That code works, but I can't figure how to put the content into one page, it ends up making a replica of the original document.该代码有效,但我无法弄清楚如何将内容放入一页中,它最终制作了原始文档的副本。

If you can help me, I thank you!如果你能帮助我,我谢谢你!

After much research I finally could resolve this.经过大量研究,我终于可以解决这个问题。 This article helped me: Merge pdf documents into a single page iText这篇文章对我有帮助:将pdf 文档合并到单页 iText

I had to modify the code a little bit.我不得不稍微修改一下代码。 There are some parts of it which I don't understand at all, and it didn't work properly.其中有些部分我根本不了解,并且无法正常工作。

So, with the edit, it ended like this:因此,通过编辑,它以这样的方式结束:

PdfReader reader = new PdfReader(pdfDirectoryPath + "temp.pdf");
Rectangle pageSize = reader.getPageSize(1);
Rectangle newSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight() * 
    reader.getNumberOfPages());
Rectangle unitSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight());
int n = (int)Math.pow(2, reader.getNumberOfPages());
int r = (int)Math.pow(2, reader.getNumberOfPages() / 2);
int c = n / r;

Document document2 = new Document(newSize);
PdfWriter writer2 = PdfWriter.getInstance(document2, new 
     FileOutputStream(pdfDirectoryPath + fileName));
document2.open();
document2.newPage();
PdfContentByte cb = writer2.getDirectContent();
PdfImportedPage page;

float offsetY, factor;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
     page = writer2.getImportedPage(reader, i);
     Rectangle currentSize = reader.getPageSize(i);
     factor = Math.min(
     unitSize.getWidth() / currentSize.getWidth(),
     unitSize.getHeight() / currentSize.getHeight());
     offsetY = newSize.getHeight() - (unitSize.getHeight() * (((i % n) / c) + 1))
              + (unitSize.getHeight() - (currentSize.getHeight() * factor)) / 2f;
     if (i > 1) {
        offsetY += 150;
     }
     cb.addTemplate(page, factor, 0, 0, factor, 0, offsetY);
}
document2.close();
reader.close();

Now works like a charm.现在就像一个魅力。 All the content I had in two pages is now in one single dynamic page without gaps between the content of different pages.我在两页中的所有内容现在都在一个单独的动态页面中,不同页面的内容之间没有间隙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM