2017-04-07 172 views
0

我使用iTextSharp写在C#中的PDF文件。 我创建了一个ClassLibrary项目并在windows应用程序中引用。 的dll实际上创建了一个名为时,一个PDF文件,但是当我打开PDF文件,查看它说There was an error opening this document. The file is damaged and could not be repaired.C#DLL生成的PDF文件被损坏

这里是我的DLL的代码来创建PDF文件

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文件,但损坏。请让我知道我做错了什么,以及如何修复我的代码。

+0

你有生成PDF - 或者你可以用word文档的工作呢?编辑:同样在大多数情况下,iTextSharp允许您使用'using'语句正确处理您的对象。 – confusedandamused

+0

是的,因为我必须生成一个'QR码'和text/csv文件不支持。 –

+0

我通常发现在MS Word中编写/生成文档然后转换为PDF更容易。你有完成文件的例子吗? – confusedandamused

回答

1

所有的事情放在一边,如果你要删除当前逻辑创建的QR码 - iTextSharp的支持几种不同类型的QR /条码创建方法。

除了不使用using声明,以下代码(以及您当前的代码)将使您开始生成QR /条码。由于我不确定您想要填写QR /条形码的方式,因为我无法为您创建一份完整的工作副本,但这应该能够让您脱颖而出!

如果有不清楚的地方,请让我知道,我可以编辑我的答案提供了更清晰的认识。

下面的代码是从官方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); 

下面是输出的一个例子:

enter image description here