c# - Write to an existing pdf file and save it as a new file using itextsharp -


i have pdf file data in it. now, want use existing pdf file , insert table it. want save file different file name existing pdf file remains same before.

note: existing pdf file not created using itextsharp. randomly downloaded. have use existing pdf file template.

public void main(list<string> jobpath) {      string oldpath = @"c:\files\sample_new.pdf";     string newpath = @"c:\files\sample_new_1.pdf";      pdfreader reader = new pdfreader(oldpath);     rectangle size = reader.getpagesizewithrotation(1);     document doc = new document(size);      filestream fs = new filestream(newpath, filemode.create, fileaccess.write);     pdfwriter writer = pdfwriter.getinstance(doc, fs);     doc.open();      foreach (var list in jobpath)     {         cb.begintext();                        doc.newpage();         doc.open();                         pdfptable table = new pdfptable(1);         table.horizontalalignment = element.align_center;         table.totalwidth = 400f;         float[] widths = new float[] { 2f };         table.setwidths(widths);         table.spacingbefore = 40f;         table.spacingafter = 30f;          pdfpcell cell = new pdfpcell();         cell.colspan = 3;         cell.horizontalalignment = 1;          table.addcell(list);          doc.add(table);          pdfimportedpage page = writer.getimportedpage(reader, 1);                                               }     doc.close(); } 

with above code able add table, contents of old file not getting copied in new file.

with following code able add content of existing pdf new one. in example first page added.

string oldpath = @"c:\files\sample_new.pdf"; string newpath = @"c:\files\sample_new_1.pdf";  pdfreader reader = new pdfreader(oldpath); itextsharp.text.rectangle size = reader.getpagesizewithrotation(1); document doc = new document(size);  filestream fs = new filestream(newpath, filemode.create, fileaccess.write); pdfwriter writer = pdfwriter.getinstance(doc, fs); doc.open();  pdfcontentbyte cb = writer.directcontent;  pdfimportedpage page = writer.getimportedpage(reader, 1); cb.addtemplate(page, 0, 0);  foreach (var list in jobpath) {     doc.newpage();     doc.open();                          pdfptable table = new pdfptable(1);     table.horizontalalignment = element.align_center;     table.totalwidth = 400f;     float[] widths = new float[] { 2f };     table.setwidths(widths);     table.spacingbefore = 40f;     table.spacingafter = 30f;      pdfpcell cell = new pdfpcell();     cell.colspan = 3;     cell.horizontalalignment = 1;     table.addcell(list);     doc.add(table); } doc.close(); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -