0

I am using below function to generate pdf using itextsharp. The problem is that the document generated has the Acro Fields as flattened (not editable by user) . Is is possible to generate/merge pdf's using itextsharp without acroform fields getting flattned?

protected override Document ConvertToPdf(XElement rptXData, Document doc, PdfWriter writer)
    {
        var rptcfg = ReportContext<ReportConfig>.Me;
        var pageHeaderFooter = ReportContext<PageHeaderFooter>.Me;
        #region values from xml
        var showheader = rptXData.Attribute("showheader").BooleanValue();
        var showfooter = rptXData.Attribute("showfooter").BooleanValue();
        var src = rptXData.Attribute("src").StringValue();
        if (src != "") src = System.IO.Path.Combine(rptcfg.ResourceRootPath, src);
        #endregion
        pageHeaderFooter.IsShowHeader = showheader;
        pageHeaderFooter.IsShowFooter = showfooter;
        var content = writer.DirectContent;

        if (src == "" || !System.IO.File.Exists(src))
        {
            ReportTable wrapper = new ReportTable(new float[] { DocWidth }, true);
            if (System.IO.Path.GetFileNameWithoutExtension(src).ToUpper() == "BLANKPAGE")
                wrapper.AddCellEx("");
            else
                wrapper.AddCellEx("Error in merging outside pages: Page " + src + " does not exist");
            doc.Add(wrapper);
            return doc;
        }

        try
        {
            // Create pdf reader
            PdfReader reader = new PdfReader(src);
            int numberOfPages = reader.NumberOfPages;

            // Iterate through all pages
            for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
            {
                // Determine page size for the current page
                doc.SetPageSize(reader.GetPageSizeWithRotation(currentPageIndex));

                // Create page and get the imported page
                doc.NewPage();
                PdfImportedPage importedPage = writer.GetImportedPage(reader, currentPageIndex);


                // Determine page orientation
                int pageOrientation = reader.GetPageRotation(currentPageIndex);
                if ((pageOrientation == 90) || (pageOrientation == 270))
                {
                    content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0,
                       reader.GetPageSizeWithRotation(currentPageIndex).Height);
                }
                else if (pageOrientation == 180) //NEW JUN13; GF-357
                {
                    content.AddTemplate(importedPage, -1f, 0, 0, -1f,
                        reader.GetPageSizeWithRotation(currentPageIndex).Width,
                        reader.GetPageSizeWithRotation(currentPageIndex).Height);
                }
                else
                {
                    content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception("PDF Merging Exception:", exception);
        }

        return doc;
    }
DevXR
  • 3
  • 2
  • 1
    This question has been answered many times before on StackOverflow. It's a pity that so many people keep finding "bad" examples. In any case: this question is a duplicate of [Copy PDF with Annotations using iText](http://stackoverflow.com/questions/26174675/copy-pdf-with-annotations-using-itext) – Bruno Lowagie Dec 07 '14 at 11:44
  • possible duplicate of [How to copy form fields with PdfWriter not PdfCopy in iTextSharp](http://stackoverflow.com/questions/10881782/how-to-copy-form-fields-with-pdfwriter-not-pdfcopy-in-itextsharp) – mkl Dec 07 '14 at 13:25

0 Answers0