[英]itextsharp: unexpected elements on copied pages
這是分割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#版本)。
您正在使用Document
, PdfWriter
和PdfImportedPage
的組合來拆分PDF。 請告訴我是誰讓您這樣做的,以便我能詛咒啟發您的人(因為我之前已經回答了數百次這個問題,而且我厭倦了重復自己的事情)。 這些類不是該工作的好選擇:
您的問題類似於這一Itext pdf合並:pdf(文本截斷)頁面之外的文檔溢出並且不顯示 。 顯然,您要拆分的原始文檔包含MediaBox和CropBox。 當您查看原始文檔時,僅顯示CropBox內部的內容。 當您查看副本時,將顯示MediaBox內的內容,並顯示“打印機標記”。 這些打印機標記顯示在發布環境中需要在何處剪切頁面。 當打印書籍或雜志時,其上打印內容的頁面通常大於最終頁面。 在組裝書或雜志之前,多余的內容會被切斷。
長話短說:閱讀文檔,用PdfCopy
替換PdfWriter
,用AddPage()
替換AddTemplate()
AddPage()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.