【问题标题】:Can iTextSharp convert PDF document to PDF/AiTextSharp 可以将 PDF 文档转换为 PDF/A
【发布时间】:2011-01-22 22:29:08
【问题描述】:

我无法在常见问题解答中找到 API 中是否存在此功能,尽管它在书中提到可能是可用的。有没有人有实现此功能的经验?

【问题讨论】:

    标签: pdf itextsharp pdfa


    【解决方案1】:

    在This thread(2007 年 6 月)上,Paulo Soares 提供了显示支持 PDF/A 的代码。这是 C# 代码(他也有一个 Java 示例):

    private void PdfATest() {
        Document doc = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("C:\\hello_A1-b_cs.pdf", FileMode.Create));
        writer.PDFXConformance = PdfWriter.PDFA1B;
        doc.Open();
    
        PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
        outi.Put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
        outi.Put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
        outi.Put(PdfName.S, PdfName.GTS_PDFA1);
    
        // get this file here: http://old.nabble.com/attachment/10971467/0/srgb.profile
        ICC_Profile icc = ICC_Profile.GetInstance("c:\\srgb.profile");
        PdfICCBased ib = new PdfICCBased(icc);
        ib.Remove(PdfName.ALTERNATE);
        outi.Put(PdfName.DESTOUTPUTPROFILE, writer.AddToBody(ib).IndirectReference);
    
        writer.ExtraCatalog.Put(PdfName.OUTPUTINTENTS, new PdfArray(outi));
    
        BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\arial.ttf", BaseFont.WINANSI, true);
        Font f = new iTextSharp.text.Font(bf, 12);
        doc.Add(new Paragraph("hello", f));
    
        writer.CreateXmpMetadata();
    
        doc.Close();
    }
    

    上面的链接包含 ICC_Profile 文件的下载。

    【讨论】:

    【解决方案2】:

    这是我解析 HTML 文件并从中创建 PDF/A 存档文档的方法,还使用样式表嵌入字体(为了避免错误:“必须嵌入所有字体。这个不是” t: Helvetica")

    希望这对某人有所帮助..

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using iTextSharp.text.pdf;
    using iTextSharp.text;
    using System.IO;
    using iTextSharp.text.html.simpleparser;
    
    namespace SaveAsPDF
    {
        class HtmlPdfConverter
        {
            public void RendererWebForm2PDFArchive(string fileName)
            {
                Console.WriteLine("Parsing HTML " + fileName);
                Document document = new Document(PageSize.A4);
    
                try
                {
                    // we create a writer that listens to the document and directs a XML-stream to a file
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName + ".pdf", FileMode.Create));
    
                    //set document as arhive
                    writer.PDFXConformance = PdfWriter.PDFA1A;
                    document.Open();
    
                    //apply stylesheet to change font (and embedd it)
                    StyleSheet styles = new StyleSheet();
                    FontFactory.Register("c:\\windows\\fonts\\verdana.ttf");
                    styles.LoadTagStyle("body", "face", "Verdana");
    
                    //prepare html
                    StreamReader sr = new StreamReader(fileName, Encoding.Default);
                    string html = sr.ReadToEnd();                                
                    html = RemoveTag(html, "<title>", "</title>");                
    
                    //convert string to stream
                    byte[] byteArray = Encoding.UTF8.GetBytes(html);
                    MemoryStream ms = new MemoryStream(byteArray);
    
                    //parse html
                    HTMLWorker htmlWorker = new HTMLWorker(document);
                    System.Collections.Generic.List<IElement> elements;
    
                    elements = HTMLWorker.ParseToList(new StreamReader(ms), styles);
                    foreach (IElement item in elements)
                    {
                        document.Add(item);
                    }
    
                    writer.CreateXmpMetadata();
                    document.Close();
                    Console.WriteLine("Done");
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.Message);
                    Console.Error.WriteLine(e.StackTrace);
                }
            }**strong text**
    

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 2016-01-21
      • 2016-03-17
      • 2015-03-21
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      相关资源
      最近更新 更多