Add spreadsheet to existing PDF using iTExtsharp

I have one PDF file in which there is one table that is dynamic, and I want to dynamically add another table below to this table in my existing PDF file.

Is there a way to add a table to an existing PDF in a specific place, when the existing table (which is not at the end of the document) is completed, then I want to add my table.

How can i add? Please suggest me a good way.

As you can see below image.

enter image description here

Thanks,

+7
source share
2 answers
using iTextSharp.text; using iTextSharp.text.pdf; /// Function which will create pdf document and save in the server folder private void ExportDataToPDFTable() { Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); try { string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf"; //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create)); doc.Open();//Open Document to write Font font8 = FontFactory.GetFont("ARIAL", 7); //Write some content Paragraph paragraph = new Paragraph("Using ITextsharp I am going to show how to create simple table in PDF document "); DataTable dt = GetDataTable(); if (dt != null) { //Craete instance of the pdf table and set the number of column in that table PdfPTable PdfTable = new PdfPTable(dt.Columns.Count); PdfPCell PdfPCell = null; //Add Header of the pdf table PdfPCell = new PdfPCell(new Phrase(new Chunk("ID", font8))); PdfTable.AddCell(PdfPCell); PdfPCell = new PdfPCell(new Phrase(new Chunk("Name", font8))); PdfTable.AddCell(PdfPCell); //How add the data from datatable to pdf table for (int rows = 0; rows < dt.Rows.Count; rows++) { for (int column = 0; column < dt.Columns.Count; column++) { PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8))); PdfTable.AddCell(PdfPCell); } } PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table doc.Add(paragraph);// add paragraph to the document doc.Add(PdfTable); // add pdf table to the document } } catch (DocumentException docEx) { //handle pdf document exception if any } catch (IOException ioEx) { // handle IO exception } catch (Exception ex) { // ahndle other exception if occurs } finally { //Close document and writer doc.Close(); } } 

Datatable example:

 private DataTable GetDataTable() { // Create an object of DataTable class DataTable dataTable = new DataTable("MyDataTable");//Create ID DataColumn DataColumn dataColumn_ID = new DataColumn("ID", typeof(Int32)); dataTable.Columns.Add(dataColumn_ID);//Create another DataColumn Name DataColumn dataColumn_Name = new DataColumn("Name", typeof(string)); dataTable.Columns.Add(dataColumn_Name); //Now Add some row to newly created dataTable DataRow dataRow;for (int i = 0; i < 5; i++) { dataRow = dataTable.NewRow(); // Important you have create New row dataRow["ID"] = i;dataRow["Name"] = "Some Text " + i.ToString(); dataTable.Rows.Add(dataRow); } dataTable.AcceptChanges(); return dataTable; } 
+4
source

The easiest way to do this is to create a new PDF file with the desired table in the right place, and then print it onto an existing PDF file. This can be done in code using (for example) the PdfStamper class, but there are also standalone tools like pdftk and many others. Do not think of it as β€œediting” a PDF file, think of it as adding something new on top of the original.

 pdftk original.pdf stamp newtable.pdf output combined.pdf 

In fact, the interesting and potentially difficult part was discussed in the @mkl source question - how do you determine the correct position of a new table. If you can come up with a geometric rule, then you are in good shape. If this involves any analysis of the source file, you should be aware that something like (seemingly) simple, since determining the number of rows in an existing table can sometimes be extremely difficult. Apparently, like the html <table> , which is similar in the content stream. An example of an actual PDF would be very helpful. A PDF image is not the same thing.

To give you some background information, parsing the layout of a PDF file is simple, as PDF readers do. Parsing PDF content is completely different and much more complicated. Just like an example, you can publish a PDF image from top to bottom, or you can draw headers and footers first, and then all the bold face elements followed by plain text. Just because two things are next to each other in a physical layout does not mean that they are next to each other in a file structure, object tree, or content stream. This is a vector graphic, not a text file or bitmap. PDF files are not intended for editing, unless the software they create provides information on how to edit the content. There are many things that seem like they should be easy, but once you understand how a PDF document is created, it makes sense that it is difficult. I am not talking about this to dissuade you, just so that you appreciate the scale of the task. If you can trace the original document from which this PDF file was created, I guarantee that you will have more success with less frustration.

+2
source

All Articles