Split PDF form into pages preserving fields

二次信任 提交于 2019-12-13 01:47:06

问题


I'm using iTextSharp to split a multipage AcroForms into singe pages AcroForm but I'm not able to 'preserve' the complete functionality of the form:

This's the code I'm using:

        using (PdfReader reader = new PdfReader(options.InputFile))
        {
            string basename = Path.GetFileNameWithoutExtension(options.InputFile);

            for (int pagenumber = 1; pagenumber <= reader.NumberOfPages; pagenumber++)
            {
                string filename;
                Document document;
                PdfCopy copy;

                document = new Document();

                filename = String.Format("{0}.{1}.pdf", basename, pagenumber);

                copy = new PdfCopy(document, new FileStream(filename, FileMode.Create));

                document.Open();

                copy.AddPage(copy.GetImportedPage(reader, pagenumber));

                document.Close();
            }

            return reader.NumberOfPages;
        }

The resulting pdf pages behave as fully working AcroForm (when used trough Acrobat Reader) but if I try to 'list' the fields inside each one of them trough iTextSharp I cannot find a single field...

P.S. I found an online service that split the form 'correctly'. Many software (PDF Split and Merge Basic for examle) behave like mine.

Where I'm wrong?

Best regards, Mike


回答1:


thks @Bruno the solution is this one. N.B. I had to reinstantiate the reader for each page since using copy.AddDocument(reader, pages) removes all pages from reader object.

        PdfReader reader = new PdfReader(options.InputFile);
        List<int> pages;
        pages = new List<int>();

        int n_pages = reader.NumberOfPages;

        string basename = Path.GetFileNameWithoutExtension(options.InputFile);

        for (int pagenumber = 1; pagenumber <= n_pages; pagenumber++)
        {
            using (PdfReader page_reader = new PdfReader(options.InputFile))
            {
                string filename;
                Document document;
                PdfCopy copy;

                pages.Clear();

                filename = String.Format("{0}.{1}.pdf", basename, pagenumber);

                document = new Document();

                copy = new PdfCopy(document, new FileStream(filename, FileMode.Create));

                copy.SetMergeFields();

                document.Open();

                pages.Add(pagenumber);

                copy.AddDocument(page_reader, pages);

                document.Close();
            }
        }

        return n_pages;


来源:https://stackoverflow.com/questions/31219403/split-pdf-form-into-pages-preserving-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!