【问题标题】:want to merge PDF form which filled several times想合并多次填写的PDF表格
【发布时间】:2012-11-09 05:08:38
【问题描述】:

我有一份由主管(或用户)填写的 PDF 表格。
我想合并生成的 PDF。
我已经完成了简单的合并,但在其中我必须先生成多个文件,然后再合并它们。
有没有什么方法可以让用户填写多个表格,然后在最终提交或打印时,我只得到所有填写的 PDF 的一个合并。

【问题讨论】:

  • 我只有一个源文件被多次填充,最后我想附加所有填充的文件以进行合并..
  • 我理解您的问题是,您有一个表单模板被许多不同的人多次使用,这可能导致文件夹中有“n”个文件。您希望合并它们。我发布的链接特定于 itextsharp 和合并。我以前用过这个,效果很好。
  • 谢谢。感谢您的回答。但这不是我需要的。我需要我一个接一个地填写表格 5 次,当我按下打印或保存时,它应该打印或保存合并的 5 个填写的表格。我不想创建 5 个单独的表单。
  • 您可以修改代码以使用 List 作为 sourceFiles 参数。 PdfReader 对象有一个重载,需要一个 byte[] ;)

标签: c# itextsharp


【解决方案1】:

试试这个合并功能来合并 PDF 文件:

public static void Merge(string[] sourceFiles, string destinationFile)
{
    try
    {
        int f = 0;
        // we create a reader for a certain document
        PdfReader reader = new PdfReader(sourceFiles[f]);
        // we retrieve the total number of pages
        int n = reader.NumberOfPages;
        //Debug.WriteLine("There are " + n + " pages in the original file.");
        // step 1: creation of a document-object
        Document document = new Document(reader.GetPageSizeWithRotation(1));
        // step 2: we create a writer that listens to the document
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
        // step 3: we open the document
        document.Open();
        PdfContentByte cb = writer.DirectContent;
        PdfImportedPage page;
        int rotation;
        // step 4: we add content
        while (f < sourceFiles.Length)
        {
            int i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
                //Debug.WriteLine("Processed page " + i);
            }
            f++;
            if (f < sourceFiles.Length)
            {
                reader = new PdfReader(sourceFiles[f]);
                // we retrieve the total number of pages
                n = reader.NumberOfPages;
                //Debug.WriteLine("There are " + n + " pages in the original file.");
            }
        }
        // step 5: we close the document
        document.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

参考:Merge PDF Files using iTextSharp

【讨论】:

【解决方案2】:

基于我在上面的 cmets 中获取的合并代码.. 我使用它来写入 http 响应的输出流。它返回一个 byte[],但您可以使用原始代码和 this 来完成工作。

public static byte[] MergeFiles(List<byte[]> sourceFiles)
    {
        Document document = new Document();
        MemoryStream output = new MemoryStream();

        try
        {
            // Initialize pdf writer
            PdfWriter writer = PdfWriter.GetInstance(document, output);

            // Open document to write
            document.Open();
            PdfContentByte content = writer.DirectContent;

            // Iterate through all pdf documents
            for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
            {
                // Create pdf reader
                PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
                int numberOfPages = reader.NumberOfPages;

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

                    // Create page
                    document.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
                    {
                        content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
                    }
                }
            }
        }
        catch (Exception exception)
        {
            throw new Exception("There has an unexpected exception" +
                  " occured during the pdf merging process.", exception);
        }
        finally
        {
            document.Close();
        }
        return output.GetBuffer();
    }

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 2012-07-28
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多