This is a migrated thread and some comments may be shown as answers.

Flattening PDF files using PdfStreamWriter

4 Answers 461 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Jay
Top achievements
Rank 1
Jay asked on 17 Mar 2020, 03:22 PM

Good day all.

I am developing an application using C# WinForms and Telerik 2020.1.218.40 that needs to take pre-filled PDF forms and display the page images as static documents. I have been able to read the PDFs and split the documents into individual pages, which when viewed using Microsoft Edge's PDF rendering, displays the forms with data, but when loaded into a RadPdfViewer, only displays the form image without any of the completed data.

Any help in displaying the form data is appreciated.

 

private static readonly string uploadPath = "H:\\TEMP\\DMSUpload\\";

string splitNameTemplate = Path.Combine(uploadPath, "SplitPage-{0}.pdf");

using (FileStream fs = new FileStream(uploadPath + "TEMP.PDF", FileMode.OpenOrCreate))
{
       RadFixedDocument document = new PdfFormatProvider().Import(fs);
       using (FileStream writeStream = new FileStream(uploadPath + "FOO.PDF", FileMode.OpenOrCreate))
       {
            PdfFormatProvider provider = new PdfFormatProvider();
            provider.Export(document, writeStream);
            using (PdfFileSource fileSource = new PdfFileSource(writeStream))
            {
                 for (int i = 0; i < fileSource.Pages.Length; i++)
                 {
                       using (PdfStreamWriter fileWriter = new PdfStreamWriter(File.OpenWrite(string.Format(splitNameTemplate, i + 1))))
                       {
                            PdfPageSource pageSource = fileSource.Pages[i];
                            fileWriter.WritePage(pageSource);
                       }
                 }
           }
      }
 }
 pdfViewer.LoadDocument(uploadPath + "SplitPage-1.pdf");

 

 

 

 

4 Answers, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 18 Mar 2020, 08:26 AM

Hello Jay,

I used the provided code snippet and create a sample project which demonstrates how to flatten the form fields in the document, split the flattened document and then export every page as a new PDF document. After that you can load these newly created documents in the PdfViewer.

We have a feature request logged in our feedback portal to provide an easier way for flattering form fields (an API): PdfProcessing: Implement API for flattening form fields. You can cast your vote for the implementation as well as subscribe to the task by clicking the Follow button to receive updates about status changes.

Regards,
Martin
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Jay
Top achievements
Rank 1
answered on 18 Mar 2020, 11:34 AM

Martin,

Thank you for your prompt reply. Your sample worked perfectly after I made one minor adjustment to handle when the widgetAppearance is returned as a null (I have variable form fields that may not get filled in [i.e. "If previous answer is yes, please explain"]).

 

 

0
Fadi
Top achievements
Rank 1
Veteran
answered on 16 Dec 2020, 05:55 PM

Seeing issues with this, most notably on fields what are rotated, they lose their rotation.

Also, it seems fonts change when setting the value, which is causing some issues. Using Tahoma on the form (not embedded) and spaces become accented E character. Some font sizes change too.

0
Martin
Telerik team
answered on 21 Dec 2020, 10:29 AM

Hi Fadi,

Thank you for the feedback. 

I saw you opened a bug report (Form Field issues) in our feedback portal providing more details on this behavior and a sample example of reproducing it. We will investigate it and will write you back. Please, let's continue our discussion on this topic in the bug report thread.

Regards,
Martin
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
PdfProcessing
Asked by
Jay
Top achievements
Rank 1
Answers by
Martin
Telerik team
Jay
Top achievements
Rank 1
Fadi
Top achievements
Rank 1
Veteran
Share this question
or