예제 #1
0
        public static void Test_PdfUpdateStream_01(string pdfFile, int objectId, string streamFile)
        {
            _tr.WriteLine("update stream for object {0} with \"{1}\" of pdf file \"{2}\"", objectId, streamFile, pdfFile);
            PB_Pdf.PdfWriter pw = null;
            Reader           r  = null;

            try
            {
                pw = new PB_Pdf.PdfWriter(pdfFile, updatePdf: true);
                IPdfObject obj = pw.reader.ReadObject(objectId);
                r          = new Reader(streamFile);
                obj.stream = r.ReadBytes((int)r.Length);
                PdfNValue length = obj.value["Length"];
                if (length == null || !length.value.isInt())
                {
                    throw new PBException("error wrong /Length of object {0}", obj.id);
                }
                length.value.valueInt = obj.stream.Length;
                pw.UpdateObject(obj);
            }
            finally
            {
                if (r != null)
                {
                    r.Close();
                }
                if (pw != null)
                {
                    pw.Close();
                }
            }
        }
예제 #2
0
        public static void Test_PdfImportStream_01(string inputPdfFile, string outputPdfFile, string streamFile)
        {
            _tr.WriteLine("import stream for object TPL1 of pdf file \"{0}\" from \"{1}\" and save pdf to \"{2}\"", inputPdfFile, streamFile, outputPdfFile);
            PB_Pdf.PdfReader pr = null;
            Reader           r  = null;

            PB_Pdf.PdfWriter pw = null;
            try
            {
                pr = new PB_Pdf.PdfReader(inputPdfFile);
                //IPdfObject obj = ReadPdfObject(pr, idObject, "Object");
                r = new Reader(streamFile);
                //obj.stream = r.ReadBytes((int)r.Length);
                //PdfNValue length = obj.value["Length"];
                //if (length == null || !length.value.isInt()) throw new PBException("error wrong /Length of object {0}", idObject);
                //length.value.valueInt = obj.stream.Length;

                pw        = new PB_Pdf.PdfWriter(outputPdfFile, FileMode.Create);
                pw.reader = pr;
                IPdfObject info = ReadPdfObject(pr, pr.Trailer["Info"].value.valueObjectId, "Info");
                pw.WriteObject(info, "Info");
                IPdfObject root = ReadPdfObject(pr, pr.Trailer["Root"].value.valueObjectId, "Root");
                pw.WriteObject(root, "Root");

                IPdfObject pages = ReadPdfObject(pr, root.value["Pages"].value.valueObjectId, "Pages");
                pw.WriteObject(pages);

                IPdfObject page1 = ReadPdfObject(pr, pages.value["Kids"].value[0].valueObjectId, "Page 1");
                pw.WriteObject(page1);

                IPdfObject page1Content = ReadPdfObject(pr, page1.value["Contents"].value.valueObjectId, "Contents page 1");
                pw.WriteObject(page1Content);

                IPdfObject page1Ressource = ReadPdfObject(pr, page1.value["Resources"].value.valueObjectId, "Resources page 1");
                pw.WriteObject(page1Ressource);

                IPdfObject page1Ressource_01 = ReadPdfObject(pr, page1Ressource.value["XObject"].value["TPL1"].value.valueObjectId, "Resources page 1 /TPL1");

                // import stream from file
                page1Ressource_01.stream = r.ReadBytes((int)r.Length);
                PdfNValue length = page1Ressource_01.value["Length"];
                if (length == null || !length.value.isInt())
                {
                    throw new PBException("error wrong /Length of object {0}", page1Ressource_01.id);
                }
                length.value.valueInt = page1Ressource_01.stream.Length;

                pw.WriteObjectWithChilds(page1Ressource_01);
            }
            finally
            {
                if (pr != null)
                {
                    pr.Close();
                }
                if (r != null)
                {
                    r.Close();
                }
                if (pw != null)
                {
                    pw.Close();
                }
            }
        }