2014-04-30 67 views
1

我有一个程序,需要使用Itextsharp和PdfWriter的PDF和打印文本到第一页。目前这一直按照我所输入文本的每个pdf的目标工作。但是,当源PDF格式为横向格式时,在将文本输入到PDF的第一页后,作者将布局旋转为纵向。我无法找到关于为什么在文本输入到pdf后为什么将默认布局更改为肖像的文档。由于原始布局是横向的,这种旋转导致信息最终在右侧被切断。iTextSharp PdfWriter当它不应该旋转页面布局

我已经看过涉及PdfStamper的其他答案,但我有麻烦操纵现有的代码来处理我正在做的事情。我在C#,pdf操作和iTextSharp编程方面相当新颖。 PDF文本的最终目标是高亮度的。

//Adds white invisible text to the pdf document that is highlightable 
public static void stamp(string pdfName, string filePath, string textToStamp) 
{ 
    //Make a Temporary copy of the original to manipulate 
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + ""; 
    File.Copy(filePath, tempPath); 
    //Make a new reader using the copied source file 
    PdfReader reader = new PdfReader(tempPath); 
    using (Document document = new Document()) 
    { 
     using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create))) 
     { 
      document.Open(); 
      int numofpages = reader.NumberOfPages; 
      for (int p = 1; p <= numofpages; p++) 
      { 
       //create the ContentByte to give the text a position on the first page 
       PdfContentByte cb = writer.DirectContent; 
       //Get the page to work with 
       PdfImportedPage page = writer.GetImportedPage(reader, p); 

       document.NewPage(); 
       cb.AddTemplate(page, 0, 20); 
       var FontColour = new BaseColor(255, 255, 255); 
       var MyFont = FontFactory.GetFont("Times New Roman", 12, FontColour); 
       //Gets the first page and sends the text to the upper left corner 
       if (p == 1) 
       { 
        if (TextToStamp!= null) 
        { 
         document.Add(new Paragraph("Hello World", MyFont)); 
        } 
       } 
       document.Close(); 
      } 
     } 
     reader.Close(); 
     File.Delete(tempPath); 
    } 

任何意见或建议您想添加,随意!谢谢

+1

你可能不应该使用'PdfWriter'。当你说你已经阅读过文档时,你的意思是你已经阅读过http://manning.com/lowagie2/samplechapter6.pdf如果是这样,请解释那一章不清楚的内容,以便我确定我当我写第三版时正确解释它。 (请注意,您的要求很不明确:您想突出显示现有文本?您将如何检索该文本的坐标?) –

+0

抱歉,要求模糊不清。目标只是将文本添加到第一页的左上角区域。压模器很难理解的是如何限制添加到特定页面的信息,但仍然保留PDF中的其余部分。除非你建议我使用阅读器仅获取第一页,然后将其重新加入到文档中。 – Ben

回答

0

使用发表布鲁诺的信息,我想出了这个解决方案。这允许在页面上打印信息,而不管布局是什么,并给它少量的定制。

public static void AddText(string pdfName, string filePath, string textToStamp, float? x = null, float? y = null) 
{ 
    //x and y are used to position the text and allow multiple different templates to use the same method 
    //Designate the Temporary source to be used 
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + ""; 
    //Copy to file to the source path 
    File.Copy(filePath, tempPath); 
    PdfReader reader = new PdfReader(tempPath); 
    iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(1); 
    //Convert the pageHeight into a float 
    int pageHeight = Convert.ToInt32(pageSize.Height); 
    PdfStamper stamper = new PdfStamper(reader, new FileStream(filePath, FileMode.Create)); 

    PdfContentByte canvas = stamper.GetOverContent(1); 
    //Set a default value if x and y have no value 
    if (x.HasValue == false) 
    { 
     x = 35; 
    } 
    if (y.HasValue == false) 
    { 
     y = 30; 
    } 
    //choose the font type 
    var FontColour = new BaseColor(255, 255, 255); 
    var MyFont = FontFactory.GetFont("Times New Roman", 10, FontColour); 
    ColumnText.ShowTextAligned 
       (canvas, Element.ALIGN_LEFT, new Phrase("Hello World", MyFont), (float)x, pageHeight - (float)y, 0); 

    stamper.Close(); 
    reader.Close(); 
    File.Delete(tempPath); 
}