在C#中将PDF页面拆分为多页 [英] Split PDF page to multiple page in C#
问题描述
我正在使用C#为Windows 8.1创建一个应用程序.在Windows.Data.Pdf中,我发现了如何在应用程序中使用PDF文件.但是我想知道是否可以将一个A3页面拆分为多个PDF文件吗?
I'm creating an application for Windows 8.1 in C#. Into Windows.Data.Pdf i've found how use PDF files in my application. But i want to know if i can split one A3 page to multiple PDF files ?
推荐答案
您不想拆分页面,而是想要平铺页面.
You don't want to split a page, you want to tile it.
在我的书的第6章(第6.2.3节)中对此进行了说明.看一下TilingHero
示例( Java / C#).在此示例中,一个大页面( hero.pdf )分为一个带有多个A4页的PDF( superman.pdf ).
This is explained in Chapter 6 of my book (section 6.2.3). Take a look at the TilingHero
example (Java / C#). In this example, one large page (hero.pdf) is split into a PDF with several A4 pages (superman.pdf).
这是一些代码:
PdfReader reader = new PdfReader(resource);
Rectangle pagesize = reader.GetPageSizeWithRotation(1);
using (Document document = new Document(pagesize)) {
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, ms);
// step 3
document.Open();
// step 4
PdfContentByte content = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(reader, 1);
// adding the same page 16 times with a different offset
float x, y;
for (int i = 0; i < 16; i++) {
x = -pagesize.Width * (i % 4);
y = pagesize.Height * (i / 4 - 3);
content.AddTemplate(page, 4, 0, 0, 4, x, y);
document.NewPage();
}
}
该数学运算对于A0页面有效.您需要对其进行调整以适应A3页面(这意味着:您所需的数学运算更容易实现).
The math is valid for an A0 page. You need to adapt it for an A3 page (meaning: the math you need is way easier to do).
您需要计算pagesize
以便它生成较小的页面,然后使用类似以下的内容:
You need to calculate pagesize
so that it results in smaller pages, and then use something like this:
using (Document document = new Document(pagesize)) {
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, ms);
// step 3
document.Open();
// step 4
PdfContentByte content = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(reader, 1);
// adding the same page 16 times with a different offset
float x, y;
for (int i = 0; i < 16; i++) {
x = -pagesize.Width * (i % 4);
y = pagesize.Height * (i / 4 - 3);
content.AddTemplate(page, x, y); // we don't scale anymore
document.NewPage();
}
}
这篇关于在C#中将PDF页面拆分为多页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!