【问题标题】:Generate PDF with iTextSharp使用 iTextSharp 生成 PDF
【发布时间】:2011-07-27 23:53:13
【问题描述】:

我正在尝试将图像添加到现有 PDF 的每一页的顶部。我曾尝试使用 PdfStamp,但由于某种原因,当我尝试从 Chrome 打印 pdf 时,我得到的只是一个黑页。 Adobe Reader 也只显示原始文件。有没有人对如何让它工作有任何想法?这是代码。

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();


    }


}

}

【问题讨论】:

  • 我实际上刚刚编写了新代码制作 PDF,Chrome 仍在打印所有黑页。这是我使用的代码。 Code

标签: c# asp.net pdf itext barcode


【解决方案1】:

您提供的代码示例甚至可以输出 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);  
  }
}

显然您需要将上面的 PATH_TO_PDF 更改为 PDF 的实际路径。还有其他方法可以实现相同的目标。例如使用PdfPageEventHelper

【讨论】:

  • 感谢您的帮助。这完美地工作,我也明白它现在是如何工作的。几周前我刚开始使用 c#,所以我遇到了很多问题。
【解决方案2】:

看看这个;

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

对于所有与 itextsharp 相关的帖子也是如此;

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

【讨论】:

  • 其实我已经读过了。在我尝试使用 PdfStamper 之前,我可以将图像写入 PDF 并毫无问题地打印。
  • 不知道 PdfStamper 是什么:S 也许问题出在 PdfStamper 上?几个月前,我在某个项目中使用了 iTextSharper,并且从未遇到过图像问题。
  • PdfStamper 用于将内容放在其他内容之上。如果我不使用它,我根本看不到图像。
猜你喜欢
  • 2011-06-29
  • 1970-01-01
  • 2022-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多