【问题标题】:C# merge PDF solution without using iTextSharpC#合并PDF解决方案不使用iTextSharp
【发布时间】:2012-10-10 09:19:05
【问题描述】:

我有一些想要使用 C# 自动填写的 PDF。我知道 iTextSharp,但我不确定用于商业用途的许可问题,并且宁愿找到不同的解决方案。

基本上,我想打开一个 PDF,指定字段(或为文本框提供坐标)并能够插入文本(可能还有小图像?)。然后我需要合并并保存pdf。

有什么好的方法可以在我不必购买/担心许可证的情况下完成此任务?

【问题讨论】:

    标签: c# pdf merge


    【解决方案1】:

    这是一个免费的开源解决方案:SharpPDF

    但老实说,iTextSharp 可能是你能找到的最好的东西。

    【讨论】:

    • 不确定“SharpPDF”和“PDFSharp”是否是完全不同的程序,但是您的链接指向的 dll 对我不起作用...我使用 PDFSharp 找到了一个可行的解决方案。但是感谢您为我指明了正确的方向!
    【解决方案2】:

    您可以尝试 Docotic.Pdf 库来完成您的任务。该库不是免费的,但可能没有什么可担心的(许可方面)。

    以下是在线提供的示例,可能会对您有所帮助:

    来自 Forms and Annotations 组的Other samples 也可以证明对您的情况有用。

    免责声明:我为图书馆的供应商工作。

    【讨论】:

    • 我也看到了,但我喜欢免费的... =)
    • 我很想对这个答案投反对票,因为 OP 特别要求提供免费选项:“任何关于实现此目的的好方法的建议,我不必购买/担心许可证?
    【解决方案3】:

    不确定 iTextSharp 的优势是什么,但我找到了一个使用 PDFSharp 的解决方案,到目前为止效果很好!由于 PDFSharp 的文档似乎有点轻松,我还发现 this thread 非常有帮助...

    如果这对任何人有帮助,这是我的示例程序:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using PdfSharp.Fonts;
    using PdfSharp.Pdf;
    using PdfSharp.Pdf.IO;
    using PdfSharp.Pdf.AcroForms;
    
    namespace PDFSharpTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void goButton_Click(object sender, EventArgs e)
            {
                //TestPDF();  //uncomment this to find out whether acroform will work correctly         
    
                //open file
                PdfDocument pdf = PdfReader.Open(@"YOURFILEPATHandNAME", PdfDocumentOpenMode.Modify);
    
                //fix some odd setting where filled fields don't always show                   SetupPDF(pdf);
    
                //find and fill fields
                PdfTextField txtEmployerName = (PdfTextField)(pdf.AcroForm.Fields["txtEmployerName"]);
                txtEmployerName.Value = new PdfString("My Name");
    
                PdfTextField txtEmployeeTitle = (PdfTextField)(pdf.AcroForm.Fields["txtEmployeeTitle"]);
                txtEmployeeTitle.Value = new PdfString("Workin'");
    
                PdfCheckBoxField chxAttached = (PdfCheckBoxField)(pdf.AcroForm.Fields["chxAttached"]);
                chxAttached.Checked = true;
    
                //save file
                pdf.Save(@"NEWFILEPATHandNAMEHERE");
            }
    
            private void SetupPDF(PdfDocument pdf)
            {
                if (pdf.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
                {
                    pdf.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
                }
                else
                {
                    pdf.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
                }
            }
    
            private void PDFTest()
            {
                OpenFileDialog ofd = new OpenFileDialog();
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    PdfDocument _document = null;
                    try { _document = PdfReader.Open(ofd.FileName, PdfDocumentOpenMode.Modify); }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "FATAL");             //do any cleanup and return             
                        return;
                    }
                    if (_document != null)
                    {
                        if (_document.AcroForm != null)
                        {
                            MessageBox.Show("Acroform is object", "SUCCEEDED");
                            //pass acroform to some function for processing          
                            _document.Save(@"C:\temp\newcopy.pdf");
                        }
                        else
                        {
                            MessageBox.Show("Acroform is null", "FAILED");
                        }
                    }
                    else
                    {
                        MessageBox.Show("Unknown error opening document", "FAILED");
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 这是 1.4 (Acrobat 6) 之后的 pdf 所必需的:forum.pdfsharp.net/viewtopic.php?f=2&t=693&p=5855#p5855 我还建议使用 PDF Clown,它具有许可,适用于商业产品
    • @RăzvanPanda 先生,您是对的...实际上我遇到了一堆关于 PDF 版本的问题,最终还是改用了 iTextSharp... PDFSharp 可以很好地创建 PDF,但是它并不是真正用于填充现有的 PDF 表单字段(这是我想要完成的)......
    • 您在使用 PDFsharp 时遇到了什么问题?我还需要使用它来填写表单字段,直到现在它都可以用于此目的,但我还没有测试所有常见情况。
    • 我主要遇到了 PDF 版本的问题...您链接中的代码似乎可以解决前几个问题,但后来我有其他 PDF 无法正常工作...尝试转换它们手动几次,无法让它工作......我决定试一试iTextSharp,并没有回头......
    • 另外,在使用 PDFSharp 填写表单字段后,我找不到任何方法来展平 PDF……使用 iTextSharp,这是一条简单的线……
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2013-03-09
    • 2023-03-22
    • 2020-02-23
    相关资源
    最近更新 更多