1

WPFアプリケーションで、FDFからPDFTemplateにデータを保存してPDFファイルを保存しようとしています。

というわけで、こんな状況です。テンプレートとして機能し、プレースホルダー (またはフィールド) を持つPDFTemplate.pdfがあります。次に、この FDF ファイルをプログラムで生成します。このFDFファイルには、 PDFTemplateに入力するために必要なすべてのフィールド名が含まれています。また、このFDFにはPDFTemaplteのファイル パスも含まれているため、開くときにどのPDFを使用するかがわかります。使用する。

ここで、 FDFをダブルクリックしようとすると、Adobeer Acrobat Readerが開き、データが入力されたPDFTemplateが表示されます。しかし、ファイル メニューを使用してこのファイルを保存することはできません。データ。

FDFデータをPDFにインポートして、サードパーティ コンポーネントを使用せずに保存できるかどうかを知りたいです。

また、これを行うのが非常に難しい場合、それを可能にする無料のライブラリの観点から、どのような解決策がありますか?

iTextSharpは商用アプリケーションには無料ではないことに気付きました。

4

1 に答える 1

2

別のライブラリPDFSharpを使用してこれを達成できました。

iTextSharp の方が優れていて使いやすいいくつかの場所を除いて、iTextSharp の動作方法と多少似ています。誰かが同様のことをしたい場合に備えて、コードを投稿しています:

//Create a copy of the original PDF file from source 
//to the destination location
File.Copy(formLocation, outputFileNameAndPath, true);

//Open the newly created PDF file
using (var pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(
                    outputFileNameAndPath, 
                    PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify))
{
   //Get the fields from the PDF into which the data 
   //is supposed to be inserted
    var pdfFields = pdfDoc.AcroForm.Fields;

    //To allow appearance of the fields
    if (pdfDoc.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
    {
        pdfDoc.AcroForm.Elements.Add(
            "/NeedAppearances", 
            new PdfSharp.Pdf.PdfBoolean(true));
    }
    else
    {
        pdfDoc.AcroForm.Elements["/NeedAppearances"] = 
            new PdfSharp.Pdf.PdfBoolean(true);
    }

    //To set the readonly flags for fields to their original values
    bool flag = false;

    //Iterate through the fields from PDF
    for (int i = 0; i < pdfFields.Count(); i++)
    {
        try
        {
            //Get the current PDF field
            var pdfField = pdfFields[i];

            flag = pdfField.ReadOnly;

            //Check if it is readonly and make it false
            if (pdfField.ReadOnly)
            {
                pdfField.ReadOnly = false;
            }

            pdfField.Value = new PdfSharp.Pdf.PdfString(
                             fdfDataDictionary.Where(
                             p => p.Key == pdfField.Name)
                             .FirstOrDefault().Value);

            //Set the Readonly flag back to the field
            pdfField.ReadOnly = flag;
        }
        catch (Exception ex)
        {
            throw new Exception(ERROR_FILE_WRITE_FAILURE + ex.Message);
        }
    }

    //Save the PDF to the output destination
    pdfDoc.Save(outputFileNameAndPath);                
    pdfDoc.Close();
}
于 2013-06-26T08:23:35.827 に答える