Beispiel #1
0
        public void Merge(InvoiceViewModel dto, FileInfo inPdf)
        {
            var tmpFilename = inPdf.FullName + "_tmp";

            File.Copy(inPdf.FullName, tmpFilename, true);
            //FileStream fs = fi.Open(FileMode.Open);
            //FileStream fs = new FileStream(tmpFilename, FileMode.Open);
            PdfReader reader = new PdfReader(tmpFilename);


            byte[] metadata = reader.Metadata;
            //reader.Close();
            //@"C:\eInvoicePdf\out.pdf"

            string ns     = "http://www.eInvoicePdf.gr/ns/XMP/";
            string ublKey = "e:file";


            XmpCore.IXmpMeta meta = null;
            if (meta != null)
            {
                meta = XmpCore.XmpMetaFactory.ParseFromBuffer(metadata);
            }
            else
            {
                meta = XmpCore.XmpMetaFactory.Create();
            }
            //string content = meta.GetPropertyString(ns, ublKey);

            //if (! XmpCore.XmpMetaFactory.SchemaRegistry.Namespaces.an)
            XmpCore.XmpMetaFactory.SchemaRegistry.RegisterNamespace(ns, "e");
            var invoice = Convert2Invoice(dto);
            var content = string.Empty;

            XmlSerializer ser = new XmlSerializer(typeof(InvoiceType));

            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    ser.Serialize(writer, invoice);
                    content = sww.ToString(); // Your XML
                }
            }

            meta.SetProperty(ns, ublKey, content);

            PdfStamper stamper = new PdfStamper(reader, new FileStream(inPdf.FullName, FileMode.Create));

            XmpCore.Options.SerializeOptions opts = new XmpCore.Options.SerializeOptions();
            metadata            = XmpCore.XmpMetaFactory.SerializeToBuffer(meta, opts);
            stamper.XmpMetadata = metadata;
            stamper.Close();
            reader.Close();
            File.Delete(tmpFilename);
        }
Beispiel #2
0
        public InvoiceDto LoadFromFile(FileInfo file)
        {
            PdfReader reader = new PdfReader(file.FullName);

            byte[] metadata = reader.Metadata;

            //XmpCore.XmpMetaFactory.
            XmpCore.IXmpMeta meta = XmpCore.XmpMetaFactory.ParseFromBuffer(reader.Metadata);

            string        ns      = "http://www.CraneSoftwrights.com/ns/XMP/";
            string        ublKey  = "c:file";
            string        content = meta.GetPropertyString(ns, ublKey);
            XmlSerializer ser     = new XmlSerializer(typeof(InvoiceType));
            StringReader  rdr     = new System.IO.StringReader(content);
            InvoiceType   invoice = (InvoiceType)ser.Deserialize(rdr);

            var dto = Convert2Dto(invoice);

            dto.Filename = file.Name;
            return(dto);
        }