Flattening PDF files using PdfStreamWriter

5 posts, 1 answers
  1. Jay
    Jay avatar
    13 posts
    Member since:
    Mar 2019

    Posted 17 Mar 2020 Link to this post

    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");

     

     

     

     

  2. Answer
    Martin
    Admin
    Martin avatar
    140 posts

    Posted 18 Mar 2020 Link to this post

    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.
  3. Jay
    Jay avatar
    13 posts
    Member since:
    Mar 2019

    Posted 18 Mar 2020 in reply to Martin Link to this post

    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"]).

     

     

  4. Fadi
    Fadi avatar
    10 posts
    Member since:
    Jul 2013

    Posted 16 Dec 2020 in reply to Jay Link to this post

    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.

  5. Martin
    Admin
    Martin avatar
    140 posts

    Posted 21 Dec 2020 Link to this post

    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/.

Back to Top