2011-04-01 17 views
11

के साथ पीडीएफ जेनरेट करें मैं मौजूदा पीडीएफ पर प्रत्येक पृष्ठ के शीर्ष पर एक छवि जोड़ने की कोशिश कर रहा हूं। मैंने पीडीएफस्टैम्प का उपयोग करने का प्रयास किया है, लेकिन किसी कारण से जब मैं क्रोम से पीडीएफ प्रिंट करने का प्रयास करता हूं तो मुझे एक ब्लैक पेज मिलता है। इसके अलावा एडोब रीडर केवल मूल दस्तावेज़ दिखाता है। क्या किसी के पास यह काम करने के बारे में कोई विचार है? कोड यहाँ है।iTextSharp

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

मैंने वास्तव में सिर्फ नया कोड लिखा है पीडीएफ और क्रोम अभी भी सभी काले पृष्ठों को प्रिंट कर रहा है। यहां इस्तेमाल किया गया कोड है। [कोड] (http://pastebin.com/VUmWfiLN) – MattAitchison

उत्तर

5

करता कोड नमूना तुम भी उत्पादन एक पीडीएफ प्रदान की? ऐसा लगता है कि आपने बारकोड छवि जोड़ने के लिए कई अलग-अलग तरीकों की कोशिश की और चीजों को भ्रमित करने वाले अतिरिक्त कोड के साथ समाप्त हो गया ... यह मुझे उलझन में डाल दिया ;-)

वैसे भी पीडीएफस्टैपर के साथ अपना लक्ष्य प्राप्त करने का एक तरीका है जैसे आपने कोशिश की ; उदाहरण 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); 
    } 
} 

स्पष्ट रूप से आप पीडीएफ की वास्तविक पथ के लिए ऊपर PATH_TO_PDF बदलने की जरूरत है। एक ही लक्ष्य को प्राप्त करने के अन्य तरीके भी हैं। उदाहरण के लिए PdfPageEventHelper का उपयोग करना।

+0

सहायता के लिए धन्यवाद। यह पूरी तरह से काम करता है और मैं यह भी समझता हूं कि यह सब अब कैसे काम करता है। मैंने कुछ हफ्ते पहले सी # के साथ शुरुआत की थी इसलिए मुझे बहुत सारे मुद्दे हैं। – MattAitchison

1

इस पर एक नज़र डालें;

http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images

इसके अलावा सभी iTextSharp संबंधित पोस्ट के लिए

इस;

http://www.mikesdotnetting.com/Category/20

+0

मैंने वास्तव में उन्हें पहले से ही पढ़ा है। मैं पीडीएफ में छवि लिख सकता हूं और इसे तब तक प्रिंट नहीं कर सकता जब तक कि मैं पीडीएफस्टैपर का उपयोग करने की कोशिश नहीं करता। – MattAitchison

+0

कोई विचार नहीं कि पीडीएफस्टैपर क्या है: एस शायद समस्या पीडीएफस्टैपर है? मैंने महीनों पहले कुछ प्रोजेक्ट जोड़े के लिए iTextSharper का उपयोग किया है और कभी भी छवियों के साथ यह समस्या नहीं थी। – tugberk

+0

पीडीएफस्टैपर का उपयोग अन्य सामग्री पर सामग्री डालने के लिए किया जाता है। अगर मैं इसका उपयोग नहीं करता हूं तो मैं छवियों को बिल्कुल नहीं देख सकता। – MattAitchison