如何在展平PDF时保留源字体

时间:2016-12-09 10:28:35

标签: pdf fonts itext xfa

我正在尝试展示基于XML Forms Architecture(XFA)的交互式PDF:

https://developers.google.com/maps/documentation/streetview/intro

它包含带有抛光编码的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;
        }
    }
}

我有试用许可证代码,我正在尝试制定解决方案,以决定是否应该支付许可费。

0 个答案:

没有答案