1

我正在尝试展平这个基于 XML 表单架构 (XFA) 的交互式 PDF:

http://www.finanse.mf.gov.pl/documents/766655/1481810/PIT-11(23)_v1-0E_2016.pdf

它包含带有波兰语编码的 Arial 字体。

用下面的代码将其展平后,我得到了一个新的 PDF,但没有 Arial 字体,也没有原始的波兰字母(改用 FreeSans 和 Helvetica)。

如何使拼合的 PDF 包含与原始 PDF 相同的字体?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml.xtra.xfa;
using iTextSharp.text;
using iTextSharp.license;

namespace PdfFiller_bon
{
    public class PdfFiller
    {
        public string LastError
        {
            get;
            private set;
        }

        public PdfFiller()
        {
            LastError = "";
        }

        public bool FillPdfFromXml(string PdfSrc, string PdfDest, string XmlSrc, string licenseFile = "")
        {
            if (!File.Exists(PdfSrc))
            {
                LastError = "PDF Form does not exist";
                return false;
            }
            if (!File.Exists(XmlSrc))
            {
                LastError = "XML file does not exist";
                return false;
            }
            PdfReader reader = null;
            PdfStamper stamper = null;
            MemoryStream ms = null;
            PdfWriter writer = null;
            try
            {
                PdfReader.unethicalreading = true;

                LicenseKey.LoadLicenseFile(licenseFile);

                ms = new MemoryStream();
                reader = new PdfReader(PdfSrc);
                stamper = new PdfStamper(reader, ms);
                stamper.Writer.CloseStream = false;
                AcroFields form = stamper.AcroFields;
                XfaForm xfa = form.Xfa;
                xfa.FillXfaForm(new FileStream(XmlSrc, FileMode.Open));
                stamper.Close();

                Document document = new Document();
                writer = PdfWriter.GetInstance(document, new FileStream(PdfDest, FileMode.Create));
                FontFactory.Register("C:\\Windows\\Fonts\\Arial.ttf", "Arial");
                Font font = FontFactory.GetFont("Arial", BaseFont.CP1250, BaseFont.EMBEDDED);
                //TODO how to feed the font to the flattener?
                XFAFlattener xfaf = new XFAFlattener(document, writer);
                ms.Position = 0;
                xfaf.Flatten(new PdfReader(ms));
                document.Close();
                writer.Close();
                ms.Close();
            }

            return true;
        }
    }
}

我有一个试用许可证代码,并且正在尝试制定一个解决方案来决定我们是否应该为许可证付费。

4

0 回答 0