如何保持itextSharp(dll)原始旋转页面

我想创build项目,从Excel中读取和写在pdf上,并打印此PDF。 从Excel文件(从单元格)读取计算机或服务器上的原始pdf文件的目录,然后在第二个pdf文件中的下一个单元格有信息。

问题是在这里,原来的pdf是横向,横向,旋转和我的程序创build复制从原来的pdf和复制pdf文件顶部excel写信息。 但是作为景观的pdf正在旋转270度。 这不行。 对于纵向旋转工作程序OK,复制OK并在副本顶部写入OK。 我的代码在哪里?

码:

public int urediPDF(string inTekst) { if (inTekst != "0") { string pisava_arialBD = @"..\debug\arial.ttf"; string oldFile = null; string inText = null; string indeks = null; //razbitje stringa string[] vhod = inTekst.Split('#'); oldFile = vhod[0]; inText = vhod[1]; indeks = vhod[2]; string newFile = @"c:\da\2"; //odpre bralnik pdf PdfReader reader = new PdfReader(oldFile); Rectangle size = reader.GetPageSizeWithRotation(reader.NumberOfPages); Document document = new Document(size); //odpre zapisovalnik pdf FileStream fs = new FileStream(newFile + "-" + indeks + ".pdf", FileMode.Create, FileAccess.Write); PdfWriter writer = PdfWriter.GetInstance(document, fs); //document.Open(); document.OpenDocument(); label2.Text = ("Status: " + reader.GetPageRotation(reader.NumberOfPages).ToString()); //določi sejo ustvarjanje pdf PdfContentByte cb = writer.DirectContent; //izbira pisave oblike BaseFont bf = BaseFont.CreateFont(pisava_arialBD, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); cb.SetColorFill(BaseColor.RED); cb.SetFontAndSize(bf, 8); //pisanje teksta v pdf cb.BeginText(); string text = inText; //izbira koordinat za zapis pravilnega teksta v pdf (720 stopinj roatacija (ležeče) in 90 stopinj (pokončno)) if (reader.GetPageRotation(1) == 720) //ležeča postavitev { cb.ShowTextAligned(1, text, 10, 450, 0); cb.EndText(); } else //pokončna postavitev { cb.ShowTextAligned(1, text + " - pokončen", 10, 750, 0); cb.EndText(); } // create the new page and add it to the pdf PdfImportedPage page = writer.GetImportedPage(reader, reader.NumberOfPages); cb.AddTemplate(page, 0, 0); // close the streams and voilá the file should be changed :) document.Close(); fs.Close(); writer.Close(); reader.Close(); } else { label2.Text = "Status: Končano zapisovanje"; return 0; } return 0; } 

图片假pdf:

假PDF

如前所述( ITextSharp包含来自输​​入文件的所有页面 , Itext pdf合并:pdf(文本截断)页面之外的文档溢出以及不显示等等),您应该阅读我的书iText in Action ( 第6章 )可以在这里findC#版本的例子)。

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

  • 你失去了所有的互动,
  • 如果页面处于横向状态,则需要自己旋转内容,
  • 您需要考虑原始页面大小,

你的问题类似于这个itextsharp:复制页面上的意外元素 。 没有任何理由你没有阅读文档? 如果你说: “我没有时间” ,如果我说我有近20年的开发经验,请相信我,而我从来没有看过“阅读文档”,这是浪费时间。

长话短说:阅读文档,用PdfCopyreplacePdfWriter ,用AddPage()replaceAddTemplate() AddPage()