Exemplo n.º 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();
                }
            }
        }
Exemplo n.º 2
0
        public static void Test_GetPdfText_08(String file)
        {
            PB_Pdf.PdfWriter pw = null;
            string file2 = zpath.PathSetFileName(file, Path.GetFileNameWithoutExtension(file) + "_new");
            _tr.WriteLine("read pdf file    \"{0}\"", file);
            _tr.WriteLine("save pdf to file \"{0}\"", file2);
            PB_Pdf.PdfReader pr = new PB_Pdf.PdfReader(file);
            pr.Trace += new TraceDelegate(_tr.WriteLine);
            try
            {
                pw = new PB_Pdf.PdfWriter(file2, 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");
                pages.value["Kids"].value.arrayValues = new IPdfValue[] { pages.value["Kids"].value.arrayValues[0] };
                pages.value["Count"].value.valueInt = 1;
                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");
                page1Content.value.objectValues.Remove("Filter");
                page1Content.stream = page1Content.deflatedStream;
                page1Content.value.objectValues["Length"].value.valueInt = page1Content.stream.Length;
                pw.WriteObject(page1Content);

                IPdfObject page1Ressource = ReadPdfObject(pr, page1.value["Resources"].value.valueObjectId, "Resources page 1");
                Dictionary<string, PdfNValue>  objectValues = new Dictionary<string, PdfNValue>();
                objectValues["TPL1"] = page1Ressource.value["XObject"].value["TPL1"];
                page1Ressource.value["XObject"].value.objectValues = objectValues;
                pw.WriteObject(page1Ressource);

                IPdfObject page1Ressource_01 = ReadPdfObject(pr, page1Ressource.value["XObject"].value["TPL1"].value.valueObjectId, "Resources page 1 /TPL1");
                page1Ressource_01.value.objectValues.Remove("Filter");
                page1Ressource_01.stream = page1Ressource_01.deflatedStream;
                page1Ressource_01.value.objectValues["Length"].value.valueInt = page1Ressource_01.stream.Length;
                pw.WriteObjectWithChilds(page1Ressource_01);

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

                //IPdfObject page1Ressource_Font_01 = ReadPdfObject(pr, page1Ressource_01.value["Resources"].value["Font"].value["F23"].value.valueObjectId, "Resources page 1 /TPL1 Font F23");
                //IPdfObject page1Ressource_Font_01_Widths = ReadPdfObject(pr, page1Ressource_Font_01.value["Widths"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 Widths");
                //IPdfObject page1Ressource_Font_01_Encoding = ReadPdfObject(pr, page1Ressource_Font_01.value["Encoding"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 Encoding");
                //IPdfObject page1Ressource_Font_01_FontDescriptor = ReadPdfObject(pr, page1Ressource_Font_01.value["FontDescriptor"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 FontDescriptor");
                //IPdfObject page1Ressource_Font_01_FontDescriptor_FontFile3 = ReadPdfObject(pr, page1Ressource_Font_01_FontDescriptor.value["FontFile3"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 FontDescriptor FontFile3");
                //IPdfObject page1Ressource_Font_01_ToUnicode = ReadPdfObject(pr, page1Ressource_Font_01.value["Widths"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 ToUnicode");

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

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

            }
            finally
            {
                pr.Close();
                if (pw != null) pw.Close();
            }
        }
Exemplo n.º 3
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();
            }
        }
Exemplo n.º 4
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();
     }
 }
Exemplo n.º 5
0
        public static void Test_GetPdfText_08(String file)
        {
            PB_Pdf.PdfWriter pw    = null;
            string           file2 = zpath.PathSetFileName(file, Path.GetFileNameWithoutExtension(file) + "_new");

            _tr.WriteLine("read pdf file    \"{0}\"", file);
            _tr.WriteLine("save pdf to file \"{0}\"", file2);
            PB_Pdf.PdfReader pr = new PB_Pdf.PdfReader(file);
            pr.Trace += new TraceDelegate(_tr.WriteLine);
            try
            {
                pw        = new PB_Pdf.PdfWriter(file2, 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");
                pages.value["Kids"].value.arrayValues = new IPdfValue[] { pages.value["Kids"].value.arrayValues[0] };
                pages.value["Count"].value.valueInt   = 1;
                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");
                page1Content.value.objectValues.Remove("Filter");
                page1Content.stream = page1Content.deflatedStream;
                page1Content.value.objectValues["Length"].value.valueInt = page1Content.stream.Length;
                pw.WriteObject(page1Content);

                IPdfObject page1Ressource = ReadPdfObject(pr, page1.value["Resources"].value.valueObjectId, "Resources page 1");
                Dictionary <string, PdfNValue> objectValues = new Dictionary <string, PdfNValue>();
                objectValues["TPL1"] = page1Ressource.value["XObject"].value["TPL1"];
                page1Ressource.value["XObject"].value.objectValues = objectValues;
                pw.WriteObject(page1Ressource);

                IPdfObject page1Ressource_01 = ReadPdfObject(pr, page1Ressource.value["XObject"].value["TPL1"].value.valueObjectId, "Resources page 1 /TPL1");
                page1Ressource_01.value.objectValues.Remove("Filter");
                page1Ressource_01.stream = page1Ressource_01.deflatedStream;
                page1Ressource_01.value.objectValues["Length"].value.valueInt = page1Ressource_01.stream.Length;
                pw.WriteObjectWithChilds(page1Ressource_01);

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

                //IPdfObject page1Ressource_Font_01 = ReadPdfObject(pr, page1Ressource_01.value["Resources"].value["Font"].value["F23"].value.valueObjectId, "Resources page 1 /TPL1 Font F23");
                //IPdfObject page1Ressource_Font_01_Widths = ReadPdfObject(pr, page1Ressource_Font_01.value["Widths"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 Widths");
                //IPdfObject page1Ressource_Font_01_Encoding = ReadPdfObject(pr, page1Ressource_Font_01.value["Encoding"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 Encoding");
                //IPdfObject page1Ressource_Font_01_FontDescriptor = ReadPdfObject(pr, page1Ressource_Font_01.value["FontDescriptor"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 FontDescriptor");
                //IPdfObject page1Ressource_Font_01_FontDescriptor_FontFile3 = ReadPdfObject(pr, page1Ressource_Font_01_FontDescriptor.value["FontFile3"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 FontDescriptor FontFile3");
                //IPdfObject page1Ressource_Font_01_ToUnicode = ReadPdfObject(pr, page1Ressource_Font_01.value["Widths"].value.valueObjectId, "Resources page 1 /TPL1 Font F23 ToUnicode");

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

                //IPdfObject page1Ressource_OPMON = ReadPdfObject(pr, page1Ressource_01.value["Resources"].value["ExtGState"].value["OPMON"].value.valueObjectId, "Resources page 1 /TPL1 ExtGState OPMON");
            }
            finally
            {
                pr.Close();
                if (pw != null)
                {
                    pw.Close();
                }
            }
        }
Exemplo n.º 6
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();
                }
            }
        }