We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Open a template and use it

Hello,

We have a pdf document which contains header and footer. We want to want that document on every new document we create.

Doing a very basic thing , but new content is not being written (doc2.pdf is the file which contains our header and footer, I have attached it)


            string infileName = "C:\\Users\\bhard\\OneDrive\\Documents\\Doc2.pdf";

            string outfileName = "C:\\Users\\bhard\\OneDrive\\Documents\\Doc2_out.pdf";

            using FileStream inputStream = new(infileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);

            using FileStream outputStream = new(outfileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);


            //Loads an existing PDF document.

            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputStream);

            //Get the first page of the document.

            PdfPageBase page = loadedDocument.Pages[0];


            //Create a page template.

            PdfPageTemplate pageTemplate = new PdfPageTemplate(page);

            //Sets the PdfPageTemplate name.

            pageTemplate.Name = "pageTemplate";

            //Sets the PdfPageTemplate is visible.

            pageTemplate.IsVisible = true;

            //Adds the page template.

            loadedDocument.PdfPageTemplates.Add(pageTemplate);

            /*


            DataTable dataTable = new DataTable();

            //Add columns to the DataTable

            dataTable.Columns.Add("ID");

            dataTable.Columns.Add("Name");

            //Add rows to the DataTable.

            dataTable.Rows.Add(new object[] { "E01", "Clay" });

            dataTable.Rows.Add(new object[] { "E02", "Thomas" });

            //Assign data source.

            PdfGrid pdfGrid = new PdfGrid();

            pdfGrid.DataSource = dataTable;

            //Draw grid to the page of PDF document.


            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 24);

            PdfBrush brush = new PdfSolidBrush(Color.Red);


            page.Graphics.DrawString("salamlar", font, brush, new PointF(10, 10));

            */

            //Create PDF graphics for the page.

            PdfGraphics graphics = page.Graphics;


            //Set the standard font.

            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            //Draw the text.

            graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));

            //Save the PDF document.

            loadedDocument.Save(outputStream);

            //Close the PDF document.

            loadedDocument.Close(true);



Attachment: Doc2_4b1f9e5d.zip

1 Reply

IJ Irfana Jaffer Sadhik Syncfusion Team February 3, 2023 11:50 AM UTC

We have investigated the reported issue using the information provided. Our analysis of your code reveals that you are using a template to create doc2 and then copying it to a new document, while also adding the text "hello world."


We were able to replicate this on our end and understand that you want to combine the contents of the output file with the header and footer from the input file. If your aim is to use the input file as a background template for another PDF document, please refer to the following KB documentation

Output document screenshot

KB documentation : https://www.syncfusion.com/kb/11735/how-to-add-page-as-background-in-an-existing-pdf-document-using-c


We have attached the current working code sample for your reference. Please try this on your end and let us know the result.


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/NetCoreSample-1539785243


If still you are facing an issue, we request you to share the modified sample,expected output pdf file with us. So that we can assist with you further in this.






Loader.
Up arrow icon