public static PdfParser Parse(Stream InputStream) { PdfParser analyzer = new PdfParser(); analyzer.inputStream = InputStream; analyzer.objectMemory = new Dictionary<int, PdfFileObject>(); analyzer.inputStream.Seek(0, SeekOrigin.Begin); string ver = new StreamReader(analyzer.inputStream).ReadLine().Substring(5); analyzer._version = double.Parse(ver, System.Globalization.CultureInfo.InvariantCulture); analyzer.startxref = GetStartxref(analyzer.inputStream); analyzer.objectMemory = analyzer.LoadObjects(analyzer.inputStream); if (analyzer.trailer == null) { analyzer.trailer = analyzer.ParseTrailer(analyzer.startxref); } analyzer.root = analyzer.trailer.GetChildObject("Root"); analyzer.root.Type = PdfObjectTypes.Root; analyzer.rootPages = analyzer.root.GetChildObject("Pages"); analyzer.isEncrypted = analyzer.trailer.GetChildObject("Encrypt") != null; analyzer.pageCount = analyzer.rootPages.GetInt32Value("Count").Value; return analyzer; }
public void StartSplitter() { this.DialogResult = DialogResult.OK; int n = 1; foreach (PdfProjectElement ppe in this.Project.Elements) { if (!ppe.Enabled) { continue; } if (ppe is PdfProjectPdf) { foreach (int page in ((PdfProjectPdf)ppe).CalculatePageSelection(true)) { using (Stream s = File.OpenRead(ppe.Path)) { PdfParser parsed = PdfParser.Parse(s); this.GenerateSplit(n, this.Project.Target, parsed, page); n++; } } } if (ppe is PdfProjectImage) { this.GenerateSplit(n, this.Project.Target, PdfParser.Parse(ImageToPDFConverter.ConvertImageToPDF(ppe.Path)), 0); n++; } } }
public static PdfParser Parse(Stream InputStream) { PdfParser analyzer = new PdfParser(); analyzer.inputStream = InputStream; analyzer.objectMemory = new Dictionary <int, PdfFileObject>(); analyzer.inputStream.Seek(0, SeekOrigin.Begin); string ver = new StreamReader(analyzer.inputStream).ReadLine().Substring(5); analyzer._version = double.Parse(ver, System.Globalization.CultureInfo.InvariantCulture); analyzer.startxref = GetStartxref(analyzer.inputStream); analyzer.objectMemory = analyzer.LoadObjects(analyzer.inputStream); if (analyzer.trailer == null) { analyzer.trailer = analyzer.ParseTrailer(analyzer.startxref); } analyzer.root = analyzer.trailer.GetChildObject("Root"); analyzer.root.Type = PdfObjectTypes.Root; analyzer.rootPages = analyzer.root.GetChildObject("Pages"); analyzer.isEncrypted = analyzer.trailer.GetChildObject("Encrypt") != null; analyzer.pageCount = analyzer.rootPages.GetInt32Value("Count").Value; return(analyzer); }
public override void AddToMerger(PdfMerger pdfMerger) { using (Stream s = new FileStream(this.Path, FileMode.Open, FileAccess.Read, FileShare.Read)) { PdfParser pa = PdfParser.Parse(s); pdfMerger.Add(pa, this.CalculatePageSelection(true)); } }
internal PdfFileObject CreateFileObject(PdfParser analyzer) { PdfFileObject pfo = new PdfFileObject(analyzer); pfo.ObjID = this.ObjID; pfo.OriginalObjectAbsolutePosition = this.Address; pfo.OriginalObjectLength = this.Length; pfo.StreamPosition = this.StreamPosition; pfo.StreamLength = this.StreamLength; return(pfo); }
public void GenerateSplit(int n, string target, PdfParser parsedPdf, int page) { target = Path.Combine(Path.GetDirectoryName(target), Path.GetFileNameWithoutExtension(target) + " - " + n.ToString("00000") + ".pdf"); using (Stream s = new FileStream(target, FileMode.Create, FileAccess.Write)) { this.merger = new PdfMerger(s); this.merger.PdfMergerProgress += new EventHandler <PdfMergerProgressEventArgs>(merger_PdfMergerProgress); this.merger.Add(parsedPdf, new int[] { page }); this.merger.Finish(); } }
public override void Analyze() { using (Stream s = new FileStream(this.Path, FileMode.Open, FileAccess.Read)) { PdfParser pfa = PdfParser.Parse(s); if (pfa.IsEncrypted) { throw new PdfEncryptedException(string.Format("{0} is an Encrypted PDF." , System.IO.Path.GetFileName(this.Path))); } //if (pfa.Version > 1.4) //{ // throw new PdfDamagedException("Not supported v1.5"); //} //if (pfa.MissingRoot) //{ // throw new PdfDamagedException("Unable to find root node"); //} this._pagecount = pfa.PageCount; } }
public void GenerateSplit(int n, string target, PdfParser parsedPdf, int page) { target = Path.Combine(Path.GetDirectoryName(target), Path.GetFileNameWithoutExtension(target) + " - " + n.ToString("00000") + ".pdf"); using (Stream s = new FileStream(target, FileMode.Create, FileAccess.Write)) { this.merger = new PdfMerger(s); this.merger.PdfMergerProgress += new EventHandler<PdfMergerProgressEventArgs>(merger_PdfMergerProgress); this.merger.Add(parsedPdf, new int[] { page }); this.merger.Finish(); } }
internal PdfFileObject(PdfParser pdfFileParser) { this.pdfFileParser = pdfFileParser; }
public override void AddToMerger(PdfMerger pdfMerger) { pdfMerger.Add(PdfParser.Parse(ImageToPDFConverter.ConvertImageToPDF(this.Path))); }
internal PdfFileObject CreateFileObject(PdfParser analyzer) { PdfFileObject pfo = new PdfFileObject(analyzer); pfo.ObjID = this.ObjID; pfo.OriginalObjectAbsolutePosition = this.Address; pfo.OriginalObjectLength = this.Length; pfo.StreamPosition = this.StreamPosition; pfo.StreamLength = this.StreamLength; return pfo; }
public void Add(PdfParser pdfParser, int[] PageNumbers) { if (this.CancelPending) return; this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, 0, 0, "Analyzing PDF Structure")); PdfFileObject[] pages = pdfParser.GetAllPages(); ArrayList selectedPages = new ArrayList(); #region gets needed objects Hashtable relatedObjects = new Hashtable(); if (PageNumbers == null) { List<int> ps = new List<int>(); for (int p = 0; p < pages.Length; p++) { ps.Add(p); } PageNumbers = ps.ToArray(); } int currentPageIndex = 1, pageCount = PageNumbers.Length; int step = Math.Max(pageCount / 20, 10); foreach (int pageNumber in PageNumbers) { if (this.CancelPending) return; if (currentPageIndex % step == 0 || currentPageIndex == pageCount) { this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, currentPageIndex, pageCount, "Analyzing Page {0} of {1}")); } PdfFileObject selectedPage = pages[pageNumber]; selectedPages.Add(selectedPage); selectedPage.PopulateRelatedObjects(relatedObjects); currentPageIndex++; } ArrayList neededObjects = new ArrayList(); neededObjects.AddRange(relatedObjects.Values); neededObjects.Sort(new PdfFileObjectNumberComparer()); #endregion #region creates IDs transformation table int objectIndex = 1; int objectCount = neededObjects.Count; step = Math.Max(objectCount / 20, 50); Hashtable transformationTable = new Hashtable(); foreach (PdfFileObject pfo in neededObjects) { if (this.CancelPending) return; if (objectIndex % step == 0 || objectIndex == objectCount) { this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, objectIndex, objectCount, "Rebuilding Indexes for object {0} of {1}")); } string hash = OptimizeStreams?pfo.Hash:null; if (hash != null && this.alreadyUsedObjects.ContainsKey(hash)) { pfo.excludedByHashComparison = true; transformationTable.Add(pfo.ObjID, (int)this.alreadyUsedObjects[hash]); } else { number++; transformationTable.Add(pfo.ObjID, number); if (hash != null) { this.alreadyUsedObjects.Add(hash, number); } } objectIndex++; } #endregion objectIndex = 1; foreach (PdfFileObject pfo in neededObjects) { if (this.CancelPending) return; if (objectIndex % step == 0 || objectIndex==objectCount) { this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, objectIndex, objectCount, "Writing object {0} of {1}")); } pfo.Transform(transformationTable); if (!pfo.excludedByHashComparison) { this.xrefs.Add(pos); this.pos += pfo.WriteToStream(this.target); } pfo.TransformatedTextPart = null; objectIndex++; } foreach (PdfFileObject selectedPage in selectedPages) { this.pageNumbers.Add(selectedPage.TransformatedObjID); } this.elementCount++; }
public void Add(PdfParser pdfParser) { this.Add(pdfParser, null); }
public void Add(PdfParser pdfParser, int[] PageNumbers) { if (this.CancelPending) { return; } this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, 0, 0, "Analyzing PDF Structure")); PdfFileObject[] pages = pdfParser.GetAllPages(); ArrayList selectedPages = new ArrayList(); #region gets needed objects Hashtable relatedObjects = new Hashtable(); if (PageNumbers == null) { List <int> ps = new List <int>(); for (int p = 0; p < pages.Length; p++) { ps.Add(p); } PageNumbers = ps.ToArray(); } int currentPageIndex = 1, pageCount = PageNumbers.Length; int step = Math.Max(pageCount / 20, 10); foreach (int pageNumber in PageNumbers) { if (this.CancelPending) { return; } if (currentPageIndex % step == 0 || currentPageIndex == pageCount) { this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, currentPageIndex, pageCount, "Analyzing Page {0} of {1}")); } PdfFileObject selectedPage = pages[pageNumber]; selectedPages.Add(selectedPage); selectedPage.PopulateRelatedObjects(relatedObjects); currentPageIndex++; } ArrayList neededObjects = new ArrayList(); neededObjects.AddRange(relatedObjects.Values); neededObjects.Sort(new PdfFileObjectNumberComparer()); #endregion #region creates IDs transformation table int objectIndex = 1; int objectCount = neededObjects.Count; step = Math.Max(objectCount / 20, 50); Hashtable transformationTable = new Hashtable(); foreach (PdfFileObject pfo in neededObjects) { if (this.CancelPending) { return; } if (objectIndex % step == 0 || objectIndex == objectCount) { this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, objectIndex, objectCount, "Rebuilding Indexes for object {0} of {1}")); } string hash = OptimizeStreams?pfo.Hash:null; if (hash != null && this.alreadyUsedObjects.ContainsKey(hash)) { pfo.excludedByHashComparison = true; transformationTable.Add(pfo.ObjID, (int)this.alreadyUsedObjects[hash]); } else { number++; transformationTable.Add(pfo.ObjID, number); if (hash != null) { this.alreadyUsedObjects.Add(hash, number); } } objectIndex++; } #endregion objectIndex = 1; foreach (PdfFileObject pfo in neededObjects) { if (this.CancelPending) { return; } if (objectIndex % step == 0 || objectIndex == objectCount) { this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount, objectIndex, objectCount, "Writing object {0} of {1}")); } pfo.Transform(transformationTable); if (!pfo.excludedByHashComparison) { this.xrefs.Add(pos); this.pos += pfo.WriteToStream(this.target); } pfo.TransformatedTextPart = null; objectIndex++; } foreach (PdfFileObject selectedPage in selectedPages) { this.pageNumbers.Add(selectedPage.TransformatedObjID); } this.elementCount++; }
static void Main(string[] args) { Console.WriteLine("GIOS PDF Splitter and Merger - Version " + Assembly.GetEntryAssembly().GetName().Version); Console.WriteLine("Copyright © 2009 Paolo Gios - www.paologios.com"); Console.WriteLine(); Console.WriteLine("Merges PDF and JPG files into a single PDF file."); Console.WriteLine(); try { if (args.Length == 0) { Help(); #if DEBUG Console.ReadLine(); #endif return; } string output = ""; for (int index = 0; index < args.Length - 1; index++) { string f = args[index].ToLower(); if (f == "output") { output = args[index + 1]; break; } } if (output == null) { return; } using (FileStream outputStream = new FileStream(output, FileMode.Create, FileAccess.Write)) { Stopwatch sw = new Stopwatch(); sw.Start(); Console.WriteLine("Creating " + output + " ..."); PdfMerger merger = new PdfMerger(outputStream); merger.PdfMergerProgress += new EventHandler <PdfMergerProgressEventArgs>(merger_PdfMergerProgress); for (int index = 0; index < args.Length; index++) { string f = args[index].ToLower(); if (f == "output") { index++; continue; } string directory = Path.GetDirectoryName(f); if (string.IsNullOrEmpty(directory)) { directory = Environment.CurrentDirectory; } string[] files = Directory.GetFiles(directory, Path.GetFileName(f), SearchOption.TopDirectoryOnly); Array.Sort(files); foreach (string file in files) { if (string.Compare(file, outputStream.Name) == 0) { continue; } string extension = Path.GetExtension(file).ToLower(); Stream fs = null; switch (extension) { case ".pdf": fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); break; case ".jpg": fs = ImageToPDFConverter.ConvertImageToPDF(file); break; default: continue; } using (fs) { Console.WriteLine(); Console.WriteLine("Adding " + file + "... "); PdfParser pa = PdfParser.Parse(fs); merger.Add(pa, null); Console.SetCursorPosition(0, Console.CursorTop); Console.WriteLine("Done. "); } } } Console.WriteLine(); Console.Write("Finishing ... "); merger.Finish(); sw.Stop(); Console.WriteLine("Finished in " + sw.ElapsedMilliseconds + " ms."); } } catch (Exception e) { Console.WriteLine(e.Message); } #if DEBUG Console.ReadLine(); #endif }