Dinesh Kumar
Dinesh Kumar

Reputation: 101

How to make version compatible of a pdf file through itextsharp in c#.net

I want to create a downlodable pdf file through itextsharp dll (ver 5.0.5). I have four textboxes which is filled by the user and click on download pdf file. Filled text paste in specific location in existing pdf file and make it downloadable. User can open or save that file. My code to create a file is:

using iTextSharp.text;
using iTextSharp.text.pdf;

Response.Clear();
Response.ContentType = "application/pdf";                                                                                        Response.AddHeader("Content-Disposition", "attachment;filename=Patient Refund Request " + txtPatientName.Text + ".pdf");

string sourceFile = Server.MapPath("~/Forms/Refund.pdf");
PdfReader reader = new PdfReader(sourceFile);    
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
PdfContentByte cb = writer.DirectContent;

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 12);
cb.BeginText();
cb.ShowTextAligned(0, txtDate.Text, 260, 655, 0);
cb.EndText();

cb.BeginText();
cb.ShowTextAligned(0, txtPatientName.Text, 260, 620, 0);
cb.EndText();

cb.BeginText();
cb.ShowTextAligned(0, txtPatDOB.Text, 260, 588, 0);
cb.EndText();

cb.BeginText();
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(new Phrase(new Chunk(txtDescription.Text, FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD))), 90, 440, 550, 36, 20, Element.ALIGN_LEFT | Element.ALIGN_TOP);
ct.Go();
cb.EndText();

PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);

document.Close();
writer.Close();
reader.Close();

This code is working fine and open the pdf file and show all contents. My issue is, this pdf is only open in adobe reader 9 or greater version. It is not opening in lower version such as adobe reader 6 or 7. I have opened it in adobe acrobat 7.0. but it could not be open. All users in my company have adobe acrobat 6 or 7.

How can I make this code version compatible that this downloadable file can open by every version minimum version 6. I hope this information is sufficient for your knowledge. Kindly give sum suggestion or code.

Thanks in advance.

Upvotes: 0

Views: 4120

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

The default version for iText and iTextSharp is PDF 1.4 which should be compatible with Adobe Acrobat 5 and greater so I'm not sure why you are having a problem with 6 and 7. But if you want to change the version you can do it this way:

writer.PdfVersion = PdfWriter.VERSION_1_2;

I would recommend setting this immediately after creating the writer object.

Upvotes: 1

Related Questions