How to tile a pdf to multiple pages with a border

走远了吗. 提交于 2020-01-25 12:05:28

问题


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:

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.

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);

    }
}

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.

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.


回答1:


Thanks to the help of @ChrisHaas in comments he pointed me to a sample that solved my problem.

How to tile a PDF with margin

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).

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();


来源:https://stackoverflow.com/questions/36034217/how-to-tile-a-pdf-to-multiple-pages-with-a-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!