Doc.Close(true) not working as expected.

Hi, I'm evaluating the trial version of Essential PDF, One of the problems that came up is that when a pdf document is opened and data is drawn on it using Essential PDF, first time it works fine but if i open new document with different parameters the data displayed is overwritten on each other. i.e. the doc still shows the old data with new data as well.
i have followed the recommended format but still no luck.
//Load an existing PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
//To-Do some manipulation
//To-Do some manipulation
//Save the document in file system
loadedDocument.Save("Output.pdf"); 
//Close the document
loadedDocument.Close(true);

9 Replies

KC Karthikeyan Chandrasekar Syncfusion Team August 3, 2018 11:25 AM UTC

Hi Shawn, 
Thank you for your interest in Syncfusion products. 
Can you provide more information about the issue you are facing. Based on the information you have provided, I have tried to reproduce the reported issue as attached in the sample below but it is working fine. It will be more helpful if you modify the below sample to reproduce the issue. 

Regards, 
Karthikeyan 



SH Shawn August 3, 2018 11:41 AM UTC

see this is the code i'm running;

            string filename = @"Reports\Templates\TestReport.pdf";
            PdfLoadedDocument pdfDoc = new PdfLoadedDocument(filename);
            PdfLoadedPage page = pdfDoc.Pages[0] as PdfLoadedPage;
            PdfGraphics graphics = page.Graphics;
            PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10);

            //Candidate Name
            graphics.DrawString("First Name".ToString(), font, PdfBrushes.Black, new PointF(385, 140));

            //Family Name
            graphics.DrawString("Last Name".ToString(), font, PdfBrushes.Black, new PointF(115, 165));

           pdfDoc.Save(filename);
            pdfDoc.Close(true);
            System.Diagnostics.Process.Start(filename);


KC Karthikeyan Chandrasekar Syncfusion Team August 3, 2018 12:03 PM UTC

Hi Shawn, 
Seems like you are opening the PDF document, modify it and save the PDF in the same location as loaded. So the original PDF document is updated with the new content, can you try saving the document in some other location. 
string filename = @"Reports\Templates\TestReport.pdf"; 
PdfLoadedDocument pdfDoc = new PdfLoadedDocument(filename); 
 
pdfDoc.Save(filename); // Instead of this 
pdfDoc.Save(@"Reports\Output\TestReport.pdf"); // try this 

Regards, 
Karthikeyan 




SH Shawn August 3, 2018 04:09 PM UTC

yeah it's working now but there is a problem and that is if pdf is already opened another pdf doesn't open when new parameters are supplied.
            string filename = @"Reports\Templates\TestReport.pdf";
            PdfLoadedDocument pdfDoc = new PdfLoadedDocument(filename);
            PdfLoadedPage page = pdfDoc.Pages[0] as PdfLoadedPage;
            PdfGraphics graphics = page.Graphics;
            PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10);

            //Candidate Name
            graphics.DrawString("First Name".ToString(), font, PdfBrushes.Black, new PointF(385, 140));

            //Family Name
            graphics.DrawString("Last Name".ToString(), font, PdfBrushes.Black, new PointF(115, 165));
            pdfDoc.Save(@"Reports\Output\TestReport.pdf")
            pdfDoc.Close(true);
            System.Diagnostics.Process.Start(@"Reports\Output\TestReport.pdf");


KC Karthikeyan Chandrasekar Syncfusion Team August 6, 2018 06:25 AM UTC

Hi Shawn, 
Yes, this is the general behavior of the C#, as the file is being open in another program I can not be resaved. You may save the output PDF with different name to ignore this exception. 

Regards, 
Karthikeyan  



SH Shawn August 9, 2018 10:00 AM UTC

Hi Karthikeyan, I have tried to save it with different name but still same problem coming. Any other solution?

            string filename = @"Reports\Templates\TestReport.pdf";
            PdfLoadedDocument pdfDoc = new PdfLoadedDocument(filename);
            PdfLoadedPage page = pdfDoc.Pages[0] as PdfLoadedPage;
            PdfGraphics graphics = page.Graphics;
            PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10);

            //Candidate Name
            graphics.DrawString("First Name".ToString(), font, PdfBrushes.Black, new PointF(385, 140));

            //Family Name
            graphics.DrawString("Last Name".ToString(), font, PdfBrushes.Black, new PointF(115, 165));
            pdfDoc.Save(@"Reports\Output\TempReport.pdf")
            pdfDoc.Close(true);
            System.Diagnostics.Process.Start(@"Reports\Output\TempReport.pdf");


KC Karthikeyan Chandrasekar Syncfusion Team August 9, 2018 10:16 AM UTC

Hi Shawn, 
Please use the below code snippet to achieve your requirement. 
string filename = @"Reports\Templates\TestReport.pdf"; 
PdfLoadedDocument pdfDoc = new PdfLoadedDocument(filename); 
PdfLoadedPage page = pdfDoc.Pages[0] as PdfLoadedPage; 
PdfGraphics graphics = page.Graphics; 
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10); 
 
//Candidate Name 
graphics.DrawString("First Name".ToString(), font, PdfBrushes.Black, new PointF(385, 140)); 
 
//Family Name 
graphics.DrawString("Last Name".ToString(), font, PdfBrushes.Black, new PointF(115, 165)); 
 
string fileName = @"Reports\Output\TempReport_" + Guid.NewGuid() + ".pdf"; 
pdfDoc.Save(fileName) 
pdfDoc.Close(true); 
System.Diagnostics.Process.Start(fileName); 

Regards, 
Karthikeyan 



SH Shawn August 10, 2018 10:20 AM UTC

Hi, i'm receiving this error after implementing your solution.
The process cannot access the file 'Project\bin\Debug\Reports\Output\TempReport_9fad6fca-6c74-404d-b1b3-e9fc7ba844cf.pdf' because it is being used by another process.


KC Karthikeyan Chandrasekar Syncfusion Team August 10, 2018 12:07 PM UTC

Hi Shawn, 
The reported issue is working fine from our side, I have attached a working sample for your reference. If the issue still reproduces from your side, please modify the sample and sent to us we will investigate it further and provide you a solution. 

Regards, 
Karthikeyan 


Loader.
Up arrow icon