2011-04-01 161 views
11

我正嘗試將圖像添加到現有PDF上每個頁面的頂部。我嘗試過使用PdfStamp,但由於某種原因,當我嘗試從Chrome打印PDF時,我所得到的只是一個黑色頁面。另外Adobe Reader只顯示原始文檔。有沒有人有任何想法如何讓它工作?這是代碼。使用iTextSharp生成PDF

public partial class MakePdf : System.Web.UI.Page 
{ 
    public MemoryStream m = new MemoryStream(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Document document = new Document(PageSize.LETTER); 

     Response.ContentType = "application/pdf"; 
     string RESULT = @"C:\Users\maitchison\Documents\Pdf\Service Report Search - 650-10-067 4114.pdf"; 
     PdfReader reader = new PdfReader(RESULT); 
     PdfStamper stamp = new PdfStamper(reader, m); 
     try 
     { 
      // Set ContentType and create an instance of the Writer. 

      Response.ContentType = "application/pdf"; 
      PdfWriter writer = PdfWriter.GetInstance(document, m); 
      writer.CloseStream = false; 

      // Open Document 

      document.Open(); 

      int n = reader.NumberOfPages; 
      int i = 1; 

      PdfContentByte cb = writer.DirectContent; 
      PdfContentByte over; 

      Barcode128 barcode128 = new Barcode128(); 
      string text2 = "650-M5-013"; 
      barcode128.Code = text2; 
      barcode128.ChecksumText = true; 
      float x = document.Right; 
      float y = document.Top; 
      iTextSharp.text.Image img2 = barcode128.CreateImageWithBarcode(cb, null, null); 

      img2.SetAbsolutePosition((x - img2.ScaledWidth), (y - img2.ScaledHeight)); 

      while (i <= n) 
      { 
       over = stamp.GetOverContent(i); 
       over.AddImage(img2); 

       i++; 

      } 

     } 

     catch (DocumentException ex) 
     { 
      Console.Error.WriteLine(ex.StackTrace); 
      Console.Error.WriteLine(ex.Message); 
     } 

     // Close document 
     stamp.Close(); 
     //document.Close(); 

     // Write pdf bytes to outputstream. 

     Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length); 
     Response.OutputStream.Flush(); 
     Response.OutputStream.Close(); 
     m.Close(); 


    } 


} 

}

+0

我其實只是寫了新的代碼,使PDF和Chrome仍然打印出所有的黑色頁面。這是我使用的代碼。 [Code](http://pastebin.com/VUmWfiLN) – MattAitchison 2011-04-01 22:35:09

回答

5

做你甚至提供輸出PDF的代碼示例?它看起來像你嘗試了許多不同的方式來添加條形碼圖像,並結束了與多餘的代碼混淆了事情......它使我困惑;-)

無論如何這裏有一種方法來實現你的目標與PdfStamper像你試過;例如HTTP Handler(ashx的):

<%@ WebHandler Language='C#' Class='addBarcodeWithStamper' %> 
using System; 
using System.IO; 
using System.Web; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

public class addBarcodeWithStamper : IHttpHandler { 
    public void ProcessRequest (HttpContext context) { 
    HttpResponse Response = context.Response; 
    Response.ContentType = "application/pdf"; 
    PdfReader reader = new PdfReader(context.Server.MapPath(PATH_TO_PDF)); 
/* 
* save __one__ instance of barcode image; 
* see MakeBarcode() method below 
*/ 
    iTextSharp.text.Image barcode = null; 
    float barcodeWidth = 0; 
    float barcodeHeight = 0; 
    using (PdfStamper stamper = new PdfStamper(reader, Response.OutputStream)) 
    { 
     int n = reader.NumberOfPages; 
     for (int i = 1; i <= n; i++) { 
     PdfContentByte cb = stamper.GetOverContent(i); 
/* 
* re-use image bytes so they are added only __once__ 
*/ 
     if (barcode == null) { 
      barcode = MakeBarcode(cb); 
      barcodeWidth= barcode.Width; 
      barcodeHeight= barcode.Height; 
     } 
/* 
* calculate in case individual page sizes are different 
*/ 
     Rectangle rect = stamper.Reader.GetPageSize(i); 
     float x = (rect.Width - barcodeWidth)/2; 
// modify/remove 10 offset as you see fit 
     float y = rect.Top - barcodeHeight - 10; 
     barcode.SetAbsolutePosition(x, y); 
     cb.AddImage(barcode); 
     }  
    } 
    } 
    public bool IsReusable { 
    get { return false; } 
    } 
// ---------------------------------------------------------------------------- 
    public iTextSharp.text.Image MakeBarcode(PdfContentByte cb) { 
    Barcode128 barcode128 = new Barcode128(); 
    string text2 = "650-M5-013"; 
    barcode128.Code = text2; 
    barcode128.ChecksumText = true;   
    return barcode128.CreateImageWithBarcode(cb, null, null); 
    } 
} 

顯然,你需要改變上面的PDF的實際路徑PATH_TO_PDF。還有其他方法可以實現相同的目標。例如使用PdfPageEventHelper

+0

感謝您的幫助。這工作完美,我也明白現在如何工作。我剛剛在幾周前開始使用c#,所以我遇到了很多問題。 – MattAitchison 2011-04-05 01:35:32

1
+0

我其實已經閱讀過它們。我可以將圖像寫入PDF並在沒有問題的情況下打印出來,直到我嘗試使用PdfStamper。 – MattAitchison 2011-04-01 21:19:32

+0

不知道PdfStamper是什麼:S可能問題是wdfth.pdfStamper?幾個月前我已經使用iTextSharper進行了一些項目,並且從未遇到過與圖像有關的問題。 – tugberk 2011-04-01 21:37:26

+0

PdfStamper用於將內容放在其他內容上。如果我不使用它,我根本看不到圖像。 – MattAitchison 2011-04-01 21:39:38

相關問題