如何将pdf平铺到带边框的多个页面 [英] How to tile a pdf to multiple pages with a border
问题描述
使用itextsharp我试图将单个(大)页面pdf文档(称为导入文档)平铺到一个新文档中,在该文档中该页面被分成几个DIN A4页面(称为输出文档)。但我想在输出的DIN A4页面周围画一个边框,只添加一个小于A4尺寸的导入文件部分。
Using itextsharp I am trying to tile a single (large) page pdf document (called import document) into a new document where this page is split into several DIN A4 pages (called output document). But I want to draw a border around the DIN A4 pages of the output and only add a smaller than A4 size part of the import document.
澄清请看图片:
For clarification please see the picture:
左边是尺寸为A3的导入文件,就像两张A4页面一样 - 旁边(黑色虚线)。这应分为带有边框的A4页面(右侧)。由于新A4页面有边框,因此导入的部分小于A4。因此,对于A3导入的输出而不是2个A4页面,我将获得6页输出。绿线是我想要绘制的边界。
On the left is the import doc in size A3 which is like two A4 pages side-by-side (black dotted line). This shall be split to A4 pages with border (on the right). Because there is a border to the new A4 page the imported section is smaller than A4. So instead of 2 A4 pages for output from the A3 import I would get 6 pages for output. The green lines are the borders I want to draw.
我已经得到了什么?
来自这个答案< a href =https://stackoverflow.com/a/24199476/505748>拆分PDF 我编写了以下代码(带按钮的WinForm),它已经正确地将pdf从a3平铺到2x A4。 (它是通用的,因此输入大小无关紧要):
What have I got already?
From this answer Split PDF I wrote the following code (WinForm with button) which already tiles the pdf correctly from a3 to 2x A4. (It is generic so input size does not matter):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//A4 210/297 millimeter
double width = (21.0 / 2.54) * 72;
double height = (29.7 / 2.54) * 72;
string filename = "test_input_a3.pdf";
filename = Path.Combine(Directory.GetCurrentDirectory(), filename);
PdfReader reader = new PdfReader(filename);
Rectangle origPagesize = reader.GetPageSizeWithRotation(1);
Rectangle newPagesize = new Rectangle((float)width, (float)height);
string outputFile = Path.Combine(Directory.GetCurrentDirectory(), "output.pdf");
FileStream ms = new FileStream(outputFile, FileMode.Create);
using (Document document = new Document(newPagesize))
{
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
PdfContentByte content = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(reader, 1);
Rectangle bounding = page.BoundingBox;
int countWidth = (int)(origPagesize.Width / newPagesize.Width) + (newPagesize.Width % origPagesize.Width > 0 ? 1 : 0);
int countHeight = (int)(origPagesize.Height / newPagesize.Height) + (newPagesize.Height % origPagesize.Height > 0 ? 1 : 0);
float x, y;
for (int i = 0; i < countHeight * countWidth; i++)
{
x = -newPagesize.Width * (i % countWidth);
y = newPagesize.Height * (i / countWidth - (countHeight - 1));
content.AddTemplate(page, x, y);
document.NewPage();
}
} // Save the document...
// ...and start a viewer.
Process.Start(outputFile);
}
}
我也可以画一个页面上的边框(不在代码中)。这是微不足道的。但我无法找到一个解决方案,如何在A4页面上拼贴小于A4的部分,并将它们放置在边框内。
I am also able to draw a border on the page (not in code). This is trivial. But I was unable to find a solution how to tile smaller than A4 parts on A4 pages and position them inside the border.
itext文档的方法描述非常简短,我无法找到任何有助于解决这个问题的方法。例如,提取大页面或类似内容的子部分的方法。
The itext documentation is very short on method description and I was unable to find anything that could help to solve this. Like for example a method to extract a subsection of a large page or something similar.
推荐答案
感谢@ChrisHaas的帮助他向我指出了一个解决我问题的样本。
Thanks to the help of @ChrisHaas in comments he pointed me to a sample that solved my problem.
它扩展了我从中得到的代码stackoverflow通过为边距位置的内容添加一个矩形剪辑(在我的情况下是我想要绘制的边框)。
It extends the code I got from the stackoverflow answer by adding a rectangle clip for the content at the position of the margin (in my case the border I want to draw).
所以我必须添加3行从AddTemplate之前的示例剪辑和围绕一切的SaveState和RestoreState,因为我想在之后的边距内绘制:
So essential I add 3 lines for clipping from the example before the AddTemplate and a SaveState and RestoreState around everything because I want to draw inside the margin afterwards:
content.SaveState();
content.Rectangle(margin, margin, newPagesizeInner.Width, newPagesizeInner.Height);
content.Clip();
content.NewPath();
content.AddTemplate(page, x, y);
content.RestoreState();
这篇关于如何将pdf平铺到带边框的多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!