itextsharp:复制页面上的意外元素 [英] itextsharp: unexpected elements on copied pages

查看:127
本文介绍了itextsharp:复制页面上的意外元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是拆分PDF文档的已知代码:

Here is known code that splits PDF document:

        try
        {
            FileInfo file = new FileInfo(@"d:\С.pdf");
            string name = file.Name.Substring(0, file.Name.LastIndexOf("."));
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(@"d:\С.pdf");
            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            int digits = 1 + (n / 10);
            System.Console.WriteLine("There are " + n + " pages in the original file.");
            Document document;
            int pagenumber;
            string filename;

            for (int i = 0; i < n; i++)
            {
                pagenumber = i + 1;
                filename = pagenumber.ToString();
                while (filename.Length < digits) filename = "0" + filename;
                filename = "_" + filename + ".pdf";
                // step 1: creation of a document-object
                document = new Document(reader.GetPageSizeWithRotation(pagenumber));
                // step 2: we create a writer that listens to the document
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(name + filename, FileMode.Create));
                // step 3: we open the document
                document.Open();
               PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page = writer.GetImportedPage(reader, pagenumber);
                int rotation = reader.GetPageRotation(pagenumber);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagenumber).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
                // step 5: we close the document
                document.Close();
            }
        }
        catch (DocumentException de)
        {
            System.Console.Error.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            System.Console.Error.WriteLine(ioe.Message);
        }

这是一个分割页面的左上角:

Here is left top corner of one splitted page:

你可以在这里看到(和其他角落)意想不到的线条,轮数..我怎样才能避免它们?

You can see here (and in other corners) unexpected lines,rounds.. How can I avoid them?

推荐答案

如前所述(ITextSharp包含输入文件中的所有页面 Itext pdf Merge:Document溢出pdf(文本截断)页面并且不显示,等等,你应该阅读第6章 iText in action (您可以在此处找到C#版本的示例。)

As explained many times before (ITextSharp include all pages from the input file, Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying, and so on), you should read chapter 6 of my book iText in Action (you can find the C# version of the examples here).

您使用的是文件 PdfWriter 和<$ c $的组合c> PdfImportedPage 分割PDF。请告诉我是谁让你这样做的,这样我就可以诅咒那些激励你的人(因为我之前已经回答过几百次这个问题了,而且我已经厌倦了重复自己)。这些课程不是这项工作的好选择:

You are using a combination of Document, PdfWriter and PdfImportedPage to split a PDF. Please tell me who made you do it this way, so that I can curse the person who inspired you (because I've answered this question hundreds of times before, and I'm getting tired of repeating myself). These classes aren't a good choice for that job:


  • 你失去了所有互动性,

  • 如果页面是横向的(你已经发现了这个),你需要自己轮换内容,

  • 你需要考虑原始页面大小,

  • ...

  • you lose all interactivity,
  • you need to rotate the content yourself if the page is in landscape (you already discovered this),
  • you need to take the original page size into account,
  • ...

您的问题与此类似 Itext pdf Merge:文档溢出pdf(文本截断)页面并且不显示。显然,您尝试拆分的原始文档包含MediaBox和CropBox。查看原始文档时,仅显示CropBox内的内容。当您查看副本时,将显示MediaBox内的内容,并显示打印机标记。这些打印机标记显示了在发布环境中需要剪切页面的位置。打印书籍或杂志时,打印内容的页面通常比最终页面大。在组装书籍或杂志之前,额外的内容会被删除。

Your problem is similar to this one Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying. Apparently the original document you're trying to split contains a MediaBox and a CropBox. When you look at your original document, only the content inside the CropBox is shown. When you look at your copy, the content inside the MediaBox is shown, unveiling "printer marks". These printer marks show where the page needs to be cut in a publishing environment. When printing books or magazines, the pages on which content is printed are usually bigger than the final page. The extra content is cut off before assembling the book or magazine.

长话短说:阅读文档,替换 PdfWriter 使用 PdfCopy ,将 AddTemplate()替换为 AddPage()

Long story short: read the documentation, replace PdfWriter with PdfCopy, replace AddTemplate() with AddPage().

这篇关于itextsharp:复制页面上的意外元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆