2013-03-08 153 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