简体   繁体   English

如何在iTextSharp中制作多页页面?

[英]How to make multisheet page in iTextSharp?

I have single page document made with iTextSharp (A4 size in portrait orientation). 我有使用iTextSharp(纵向A4尺寸)制成的单页文档。 Now I copy first page, paste it as second - there are two same pages. 现在,我复制第一页,粘贴为第二页-有两个相同的页面。 In Acrobat Reader there is option to print multiple sheets per page - so I can print those 2 pages on 1 in landscape orientation. 在Acrobat Reader中,可以选择每页打印多张纸-因此,我可以横向将这两页打印在1上。

How to achieve the same effect, using only iTextSharp? 仅使用iTextSharp如何实现相同的效果?

Please read the tutorial on how to use iText 7, more specifically Chapter 6: Reusing existing PDF documents 请阅读有关如何使用iText 7的教程,尤其是第6章:重用现有的PDF文档。

In that chapter, you'll find an example called TheGoldenGateBridge_N_up : 在该章中,您将找到一个名为TheGoldenGateBridge_N_up的示例:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfDocument sourcePdf = new PdfDocument(new PdfReader(SRC));
//Original page
PdfPage origPage = sourcePdf.getPage(1);
Rectangle orig = origPage.getPageSize();
PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf);
//N-up page
PageSize nUpPageSize = PageSize.A4.rotate();
PdfPage page = pdf.addNewPage(nUpPageSize);
PdfCanvas canvas = new PdfCanvas(page);
//Scale page
AffineTransform transformationMatrix = AffineTransform.getScaleInstance(
    nUpPageSize.getWidth() / orig.getWidth() / 2f,
    nUpPageSize.getHeight() / orig.getHeight() / 2f);
canvas.concatMatrix(transformationMatrix);
//Add pages to N-up page
canvas.addXObject(pageCopy, 0, orig.getHeight());
canvas.addXObject(pageCopy, orig.getWidth(), orig.getHeight());
canvas.addXObject(pageCopy, 0, 0);
canvas.addXObject(pageCopy, orig.getWidth(), 0);
// close the documents
pdf.close();
sourcePdf.close();

In this example, we add 4 pages of an existing PDF to one page. 在此示例中,我们将现有PDF的4页添加到一页。 The principle is called N-upping in which you replace N by a power of 2. In the example, we do 4-upping; 该原理称为N-uping ,其中您将N替换为2的幂。 you want 2-upping. 你想2-upping。 Changing the 4-up example into a 2-up example is only a matter of applying some simple Math. 将4-up示例转换为2-up示例仅是应用一些简单的Math的问题。

You will also benefit from reading this FAQ entry: How to convert an A4 size PDF to a PDF booklet? 您还将从阅读此常见问题解答条目中受益: 如何将A4大小的PDF转换为PDF手册? If you are still using an old version of iText, you can read the iText 5 version of the FAQ entry . 如果您仍在使用旧版本的iText,则可以阅读FAQ条目iText 5版本

Note: I see a down-vote and an important comment. 注意:我看到了反对意见和重要评论。 Please note that your question is seriously flawed because you don't mention which version of iText you are using (I'm assuming the most recent one), nor do you post any code that shows what you've tried (that's not done on Stack Overflow). 请注意,您的问题存在严重缺陷,因为您没有提到您正在使用哪个版本的iText(我假设是最新版本),也没有张贴任何显示您已尝试过的代码(未完成)堆栈溢出)。

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

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