将PDF表单拆分为保留字段的页面 [英] Split PDF form into pages preserving fields

查看:140
本文介绍了将PDF表单拆分为保留字段的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp将多页AcroForms拆分为单页AcroForm,但我无法保留表单的完整功能:

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;
        }

生成的pdf页面表现为完全正常工作的AcroForm(通过Acrobat Reader使用时)但如果我尝试通过iTextSharp'列出'其中每个字段内的字段,我找不到单个字段...

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

PS我发现了一种在线服务,可以正确地分割表格。许多软件(PDF Split和Merge Basic for examle)的行为与我的相似。

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

我哪里错了?

最好的问候,
迈克

Best regards, Mike

推荐答案

thk @Bruno解决方案就是这个。注:因为使用copy.AddDocument(reader,pages)从reader对象中删除所有页面,所以我必须为每个页面重新实现阅读器。

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;

这篇关于将PDF表单拆分为保留字段的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆