itextsharp:复制页面上的意外元素

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

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

这是一个分割页面的左上角: 在此处输入图像描述

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

正如之前多次解释的那样( ITextSharp包含输入文件中的所有页面 , Itext pdf合并:文档溢出pdf(文本截断)页面而不显示 ,等等),你应该阅读我的书iText in Action的 第6章 (你可以在这里找到C#版本的例子)。

您正在使用DocumentPdfWriterPdfImportedPage的组合来拆分PDF。 请告诉我是谁让你这样做的,这样我就可以诅咒那些激励你的人(因为我之前已经回答过几百次这个问题了,而且我已经厌倦了重复自己)。 这些课程不适合这项工作:

  • 你失去了所有的互动性,
  • 如果页面是横向的(你已经发现了这个),你需要自己旋转内容,
  • 您需要考虑原始页面大小,

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

简而言之:阅读文档,用PdfCopy替换PdfWriter ,用AddPage()替换AddTemplate() AddPage()