I am using iTextSharp
to write pdf file in c#.
I have created a ClassLibrary
project and referenced in windows application.
the dll actually created a pdf file when called, but when I open the pdf file to view it says There was an error opening this document. The file is damaged and could not be repaired.
Here is my dll code to create pdf file
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);
}
}
and this is how I am calling it in windows application
Testdll.Class1 m = new Testdll.Class1();
m.CreatePDF();
MessageBox.Show("Done");
it creates the pdf file but damaged. please let me know what am I doing wrong and how can I fix my code.
All things aside if you were to remove your current logic for creating QR codes - iTextSharp supports a few different types of QR/Barcode creation methods.
Aside from not making use of using
statements the following code (along with your current code) will get you started with generating QR/Barcodes. As I'm not sure what you want to populate the QR/Barcode with I'm unable to create an exact working copy for you, but this should get you off the ground!
If anything is unclear please let me know and I can edit my answer to provide a more clear understanding.
The below code is taken from the official iText docs (originally in Java), but I changed it slightly for your purposes.
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);
Below is an example of the output: