c# dll生成的pdf文件损坏

c# dll generated pdf file is damaged

我正在使用 iTextSharp 在 c# 中编写 pdf 文件。 我创建了一个 ClassLibrary 项目并在 windows 应用程序中引用。 dll 在调用时实际上创建了一个 pdf 文件,但是当我打开 pdf 文件查看它时说 There was an error opening this document. The file is damaged and could not be repaired.

这是我创建 pdf 文件的 dll 代码

public class Class1
{
    public void CreatePDF()
    {
        // Orignal
        string filename = "Desktop\TEST_" + DateTime.Now.ToString("yyyyMMddhhmm") + ".pdf";
        FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
        Document doc = new Document(PageSize.A4, 10, 10, 10, 10);
        doc.SetMargins(10, 10, 10, 10);
        PdfWriter writer = PdfWriter.GetInstance(doc, fs);
        doc.Open();
        doc.NewPage();

        BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
        iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 9, iTextSharp.text.Font.ITALIC, iTextSharp.text.BaseColor.RED);

        iTextSharp.text.Font font = FontFactory.GetFont(FontFactory.HELVETICA, 9, BaseColor.BLACK);
        iTextSharp.text.Font fontSmall = FontFactory.GetFont(FontFactory.HELVETICA, 2, BaseColor.BLACK);
        iTextSharp.text.Font fontBold = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 9, BaseColor.BLACK);

        Paragraph ClientName = new Paragraph("Client Name :" + " " + "Client", font);
        Paragraph Date = new Paragraph("Date :" + " " + DateTime.Today.ToShortTimeString(), font);


        doc.Add(ClientName);
        doc.Add(Date);

        LineSeparator ls = new LineSeparator();
        ls.LineWidth = 7f;
        doc.Add(new Chunk(ls, font.IsBold()));

        Paragraph some= new Paragraph("Some Data", fontBold);

        doc.Add(some);

        doc.Add(new Chunk(ls, font.IsBold()));

        DmtxImageEncoder encoder = new DmtxImageEncoder();
        DmtxImageEncoderOptions options = new DmtxImageEncoderOptions();
        options.SizeIdx = DmtxSymbolSize.DmtxSymbol18x18;

        options.Scheme = DmtxScheme.DmtxSchemeC40;
        Bitmap qrbitmap = encoder.EncodeImage("123456789", options);

        iTextSharp.text.Image qrpic = iTextSharp.text.Image.GetInstance(qrbitmap, System.Drawing.Imaging.ImageFormat.Jpeg);

        qrpic.ScalePercent(15f);

        doc.Add(qrpic);

        doc.Close();


        //System.Diagnostics.Process.Start(filename);
    }
}

这就是我在 windows 应用程序中调用它的方式

Testdll.Class1 m = new Testdll.Class1();
        m.CreatePDF();
        MessageBox.Show("Done");

它创建了 pdf 文件但已损坏。请让我知道我做错了什么以及如何修复我的代码。

如果您要删除当前创建 QR 码的逻辑 - iTextSharp 支持几种不同类型的 QR/Barcode 创建方法。

除了不使用 using 语句之外,以下代码(以及您当前的代码)将帮助您开始生成 QR/Barcode。由于我不确定你想用什么填充 QR/Barcode,我无法为你创建一个精确的工作副本,但这应该让你离开地面!

如果有任何不清楚的地方,请告诉我,我可以编辑我的答案以提供更清晰的理解。

以下代码取自官方iText docs(原在Java),但为了您的需要我稍作改动

          doc.Add(new Paragraph("Barcode PDF417"));
        BarcodePDF417 pdf417 = new BarcodePDF417();
        String text = "Call me Ishmael. Some years ago--never mind how long "
            + "precisely --having little or no money in my purse, and nothing "
            + "particular to interest me on shore, I thought I would sail about "
            + "a little and see the watery part of the world.";
        pdf417.SetText(text);
        Image img = pdf417.GetImage();
        img.ScalePercent(50, 50 * pdf417.YHeight);
        doc.Add(img);

        doc.Add(new Paragraph("Barcode Datamatrix"));
        BarcodeDatamatrix datamatrix = new BarcodeDatamatrix();
        datamatrix.Generate(text);
        img = datamatrix.CreateImage();
        doc.Add(img);

        doc.Add(new Paragraph("Barcode QRCode"));
        BarcodeQRCode qrcode = new BarcodeQRCode("Moby Dick by Herman Melville", 1, 1, null);
        img = qrcode.GetImage();
        doc.Add(img);

下面是输出示例: