2013-03-08 149 views
1

我想创建该项目,从Excel中读取并写入pdf并打印此pdf。 从Excel文件(从单元格)读取电脑或服务器上的原始pdf文件的目录,下一个单元格有信息在第二个pdf文件的顶部。如何保持itextSharp原始旋转页面(dll)

问题是在这里,原始pdf是水平的,风景,旋转和我的程序从原始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:

Fake PDF

+0

您不幸发现不应该使用这种示例代码。如果你想在它上面添加PDF和图章,可以将代码基于[Web化iTextSharp示例](http://kuujinbo.info/iTextInAction2Ed/index.aspx)[StampText.cs](http:// kuujinbo。 info/iTextInAction2Ed/index.aspx?ch = Chapter06&ex = StampText)[iText in Action - 第2版] [http://www.manning.com/lowagie2/samplechapter6.pdf] /itextpdf.com/book/index.php)。而不是'PdfWriter'it使用'PdfStamper',它原样使用源页面。 – mkl 2013-03-08 11:01:53

回答

3

正如前面解释过多次(ITextSharp include all pages from the input fileItext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying,等等),你应该看过我的书iText in Actionchapter 6(你可以找到示例here的C#版本)。

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

  • 你失去所有的交互性,
  • 你需要的内容自己旋转,如果页面是在景观,
  • 你需要采取原来的页面大小兼顾,
  • ...

你的问题是与此类似itextsharp: unexpected elements on copied pages。你有没有任何理由不阅读文档?如果你说:“我没有时间”,如果我说我拥有将近20年的开发经验,并且我从来没有见过“阅读文档”是浪费时间,请相信我。

长话短说:阅读说明文件,用PdfCopy替换PdfWriter,用AddPage()替换AddTemplate()

+0

你能否修复链接到sourceforge(tinyurl链接)的链接? – nhahtdh 2015-07-08 11:03:00

+0

@nhahtdh完成!现在链接应该指向C#示例。 – 2015-07-08 21:05:58