iTextSharp如何将自定义属性添加到PDF-A?

时间:2019-01-29 05:02:23

标签: c# vb.net itext metadata pdfa

基本上,我具有许多Custom > Properties的原始XFA文件,可以将其转换为PDF-A,但是转换后所有属性都丢失了。

这是我的ConvertToPDFa代码:

 public byte[] ConvertToPDFa3(byte[] OriginPDF,byte[] FileStore,string XML)
    {
        //***************************************
        // Convert PDF to PDFa3 and Attach XML .
        //***************************************
        //Create Reader for Reading PDF in  byte[]

        using (var reader = new PdfReader(OriginPDF))
        {
            // Open Stream for Converting
            using (var Convertstream = new MemoryStream())
            {
                // New Doc for PDF/A-3
                Document pdfAdocument = new Document();

                //Init instance for pdfAdocument
                PdfAWriter writer = PdfAWriter.GetInstance(pdfAdocument, Convertstream, PdfAConformanceLevel.PDF_A_3U);
                writer.CreateXmpMetadata();

                if (!pdfAdocument.IsOpen())
                    pdfAdocument.Open();

                PdfContentByte cb = writer.DirectContent; // Holds the PDF data 
                //Count original PDF pages and uses for pdfa-3
                PdfImportedPage page;
                int pageCount = reader.NumberOfPages;
                for (int i = 0; i < pageCount; i++)
                {
                    pdfAdocument.NewPage();
                    page = writer.GetImportedPage(reader, i + 1);
                    cb.AddTemplate(page, 0, 0);
                }
                //Create Output Intents 
                ICC_Profile icc = ICC_Profile.GetInstance(AppDomain.CurrentDomain.BaseDirectory + "res\\sRGB Color Space Profile.icm");
                writer.SetOutputIntents("sRGB IEC61966-2.1", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

                //Embedded File to PDFa3 
                writer.AddFileAttachment("XML for : " + certRefNumber, FileStore, XML, lblRefNo.Text + ".xml");
                //Close all file to finish 
                pdfAdocument.Close();
                reader.Close();

                //Test writes output in Phys path
                //File.WriteAllBytes(@"OutPDFa.pdf", Convertstream.ToArray());
                byte[] filledPDfa = Convertstream.ToArray();
                return filledPDfa;
            }     
        }
        // End Convert and Attached
    }

我尝试使用以下代码获取原始属性:

static Dictionary<string, string> GetPdfProperties(byte[] originfile)
    {
        Dictionary<string, string> propertyInfo = null;

        using (PdfReader reader = new PdfReader(originfile))
        {
            propertyInfo = reader.Info;
            reader.Close();
        }

        return propertyInfo;
    }

然后将原始属性复制到PDF-A:

static byte[] CopyProps(byte[] fileinput,byte[] fileOrigin,PdfAWriter wr)
    {
            using (var rd = new PdfReader(fileinput))
            {
                using (var outp = new MemoryStream())
                {
                    using (var stamper = new PdfAStamper(rd, outp,PdfAConformanceLevel.PDF_A_3U))
                    {
                        var info = rd.Info;
                        Dictionary<string, string> propertyInfo = GetPdfProperties(fileOrigin);
                    foreach (KeyValuePair<string, string> property in propertyInfo)
                    {
                        info[property.Key] = property.Value;
                    }
                    stamper.MoreInfo = info;

                    using (var ms = new MemoryStream())
                        {
                            wr.CreateXmpWriter(ms,info); //****error was here
                            stamper.XmpMetadata = ms.ToArray();
                        }
                    }
                return outp.ToArray();
                } 
            }  
    }

,但由于受保护级别而发现错误是无法解决的 我应该将错误行放置到另一个行吗?

对于这些问题还有其他解决方案吗? 请帮助:(

我使用iTextSharp 5.5.13

0 个答案:

没有答案