public int Add(string file_add, int[] page_list) { // perform a garbage collection if processing large documents if (this.inputDocument != null && this.outputDocument.PageCount > 1000) { this.inputDocument = null; GC.Collect(); } this.inputDocument = CompatiblePdfReader.Open(file_add, PdfDocumentOpenMode.Import); int count = this.inputDocument.PageCount; int added_count = 0; for (int idxInputPage = 0; idxInputPage < count; idxInputPage++) { for (int idxPageList = 0; idxPageList < page_list.Length; ++idxPageList) { if (page_list[idxPageList] == idxInputPage) { this.outputDocument.AddPage(this.inputDocument.Pages[idxInputPage]); ++added_count; } } } // tbd - is this a good idea? if (this.inputDocument.Version > this.outputDocument.Version) { this.outputDocument.Version = this.inputDocument.Version; } this.lastPageList = page_list; if (added_count == count) { this.lastPageList = null; } this.lastAddedCount = added_count; return(added_count); }
private void createDivertPDF(List <AirfieldInformation> suitableDiverts) { //Setup up fonts, brushes, etc const string facename = "Microsoft Sans Serif"; XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.WinAnsi, PdfFontEmbedding.Default); XFont[] fonts = new XFont[] { new XFont(facename, 6.8, XFontStyle.Regular, options), new XFont(facename, 6.8, XFontStyle.Bold, options), new XFont(facename, 6.8, XFontStyle.Italic, options) }; XBrush brush = XBrushes.Black; XStringFormat format = new XStringFormat(); format.Alignment = XStringAlignment.Near; //Create variables for drawwing double lineSpace; XRect rect = new XRect(0, 0, 0, 0); double topOfWriting, bottomOfWriting, leftOfWriting, columnSpacing; double x, y; //Create the PDF document PdfDocument doc = new PdfDocument(); //Add the first page PdfPage p = new PdfPage(); p.Size = PdfSharp.PageSize.A4; p.Orientation = PdfSharp.PageOrientation.Portrait; doc.AddPage(p); //add diverts bookmark int pageNo = 0; PdfOutline.PdfOutlineCollection outlines = doc.Outlines; outlines.Add("Divert Data", doc.Pages[pageNo]); XGraphics xgr = XGraphics.FromPdfPage(p); topOfWriting = p.Height * 0.02; bottomOfWriting = p.Height * 0.97; leftOfWriting = p.Width * 0.02; columnSpacing = p.Width * 0.90 * 0.26; y = topOfWriting; x = leftOfWriting; lineSpace = fonts[0].GetHeight(xgr); //Loop through and add each of the diverts for (int d = 0; d < suitableDiverts.Count; d++) { //Setup entry for printing string[][] printData = new string[][] { fieldPrint(suitableDiverts[d].Airfield), runwayPrint(suitableDiverts[d].Runways), navPrint(suitableDiverts[d].NavigationAids), commsPrint(suitableDiverts[d].Communications) }; int maxDataLength = printData.Max(l => l.Length); int remainingRoom = Convert.ToInt32((bottomOfWriting - y) / lineSpace); //If entry won't fit on remainder of page, create a new page if (maxDataLength > remainingRoom) { //Add function to prevent the same code appearing twice p = new PdfPage(); p.Size = PdfSharp.PageSize.A4; p.Orientation = PdfSharp.PageOrientation.Portrait; doc.AddPage(p); pageNo++; xgr = XGraphics.FromPdfPage(p); topOfWriting = p.Height * 0.03; bottomOfWriting = p.Height * 0.97; leftOfWriting = p.Width * 0.03; columnSpacing = p.Width * 0.90 * 0.26; y = topOfWriting; x = leftOfWriting; } for (int a = 0; a < maxDataLength; a++) { for (int b = 0; b < printData.Length; b++) { if (a < printData[b].Length) { //Improve the printing to remove this section XFont f = fonts[0]; if (a == 0 && b == 0) { f = fonts[1]; } if (a == 1 && b == 0) { f = fonts[2]; } if (a % 2 == 1 && b == 2) { f = fonts[2]; } //Improve!!!^^^ x = leftOfWriting + columnSpacing * b; rect = new XRect(x, y, columnSpacing, lineSpace); xgr.DrawString(printData[b][a], f, brush, rect, format); } } y += lineSpace; } y += lineSpace; } //Get common divert plates List <Plate> commonPlates = new List <Plate>(); foreach (AirfieldInformation div in suitableDiverts) { for (int f = 0; f < div.Plates.Count; f++) { if (div.Plates[f].Type == "Minimums" && !div.Plates[f].Name.Contains(div.Airfield.Name)) { if (!commonPlates.Exists(a => a.Location == div.Plates[f].Location)) { commonPlates.Add(div.Plates[f]); } div.Plates.Remove(div.Plates[f]); f--; } } } commonPlates.Sort((a, b) => a.Name.CompareTo(b.Name)); // Add common plates outlines = doc.Outlines; for (int f = 0; f < commonPlates.Count; f++) { PdfDocument inputDocument = CompatiblePdfReader.Open(commonPlates[f].Location); for (int k = 0; k < inputDocument.PageCount; k++) { doc.AddPage(inputDocument.Pages[k]); //bookmarks pageNo++; if (k == 0) { if (f == 0) { outlines.Add("Minimums", doc.Pages[pageNo]); outlines = outlines[outlines.Count - 1].Outlines; } outlines.Add(commonPlates[f].Name, doc.Pages[pageNo]); } } } //Add divert plates for (int d = 0; d < suitableDiverts.Count; d++) { for (int f = 0; f < suitableDiverts[d].Plates.Count; f++) { outlines = doc.Outlines; PdfDocument inputDocument = CompatiblePdfReader.Open(suitableDiverts[d].Plates[f].Location); for (int k = 0; k < inputDocument.PageCount; k++) { doc.AddPage(inputDocument.Pages[k]); //bookmarks pageNo++; if (k == 0) { if (f == 0) { outlines.Add(suitableDiverts[d].Airfield.Name, doc.Pages[pageNo]); } outlines = outlines[outlines.Count - 1].Outlines; string typ = suitableDiverts[d].Plates[f].Type; if (f != 0 && !(typ == "Supplement" || typ == "Airport Diagram" || typ == "LAHSO" || typ == "Hotspot")) { if (typ != suitableDiverts[d].Plates[f - 1].Type) { outlines.Add(typ, doc.Pages[pageNo]); } outlines = outlines[outlines.Count - 1].Outlines; } outlines.Add(suitableDiverts[d].Plates[f].Name, doc.Pages[pageNo]); } } } } doc.Save(@"C:\Users\Mike\Desktop\Output.pdf"); doc.Close(); }
// This function adapted from the sample at http://www.pdfsharp.net/PDFsharp/index.php?option=com_content&task=view&id=52&Itemid=60 // Document is titled "Concatenate Documents" and is part of the PDFSharp Samples collection. /// <summary> /// Imports all pages from a list of documents. /// </summary> public PdfDocument MergeDocuments(string[] files) { PdfDocument outputDocument = null; ShowWaitingBox waiting = new ShowWaitingBox("Molimo sačekajte...", "U toku je kreiranje PDF dokumenta. Molimo sačekajte... "); waiting.Start(); try { // Get some file names if (files.Length == 0) { return(null); } // Open the output document outputDocument = new PdfDocument(); int n = 0, p = 1; // Iterate files foreach (string file in files) { if (!File.Exists(file)) { waiting.Stop(); return(null); } // Open the document to import pages from it. PdfDocument inputDocument = CompatiblePdfReader.Open(file, PdfDocumentOpenMode.Import); if (inputDocument == null) { waiting.Stop(); return(null); } // Iterate pages int count = inputDocument.PageCount; for (int idx = 0; idx < count; idx++) { if (stampaj[n] == null || stampaj[n][idx + 1] == true) { // Get the page from the external document... PdfPage page = inputDocument.Pages[idx]; // ...and add it to the output document. outputDocument.AddPage(page); waiting.Update("U toku je kreiranje PDF dokumenta. Molimo sačekajte... " + p++); } } n++; } } catch (Exception e) { outputDocument = null; } if (outputDocument == null) { waiting.Stop(); return(null); } waiting.Stop(); return(outputDocument); }
private void createPDFTerminals(List <airfieldPlateData> inputAirfields, int[] areas) { //Setup PdfDocument pdfDoc = new PdfDocument(); PdfOutline.PdfOutlineCollection outlines = pdfDoc.Outlines; int intPageNo = 0; int intPagesAdded = 0; PdfDocument inputDocument; //Cover page PdfPage p = new PdfPage(); p.Size = PdfSharp.PageSize.A5; pdfDoc.AddPage(p); createCoverPage(p, areas); outlines.Add("Cover", pdfDoc.Pages[intPageNo]); intPagesAdded++; intPageNo = intPagesAdded; //Special Notices bool mainBookmark = false; for (int m = 0; m < areas.Length; m++) { inputDocument = CompatiblePdfReader.Open(strDiscLoc + areaDiscLocations[areas[m]][0]); for (int k = 0; k < inputDocument.PageCount; k++) { pdfDoc.AddPage(inputDocument.Pages[k]); intPagesAdded++; } //Bookmark outlines = pdfDoc.Outlines; if (!mainBookmark) { outlines.Add("Special Notices", pdfDoc.Pages[intPageNo]); mainBookmark = true; } outlines = outlines[outlines.Count - 1].Outlines; outlines.Add(areaDiscLocations[areas[m]][4], pdfDoc.Pages[intPageNo]); //add to page count intPageNo = intPagesAdded; } //Legends inputDocument = CompatiblePdfReader.Open(strDiscLoc + strTerminalsLegend); List <bookmarksLayout> inputBookmarks = importPDFDocumentBookmarks(strDiscLoc + strTerminalsLegend); mainBookmark = false; bool includePage = false; for (int k = 0; k < inputDocument.PageCount; k++) { bookmarksLayout inputLevelOneBook = inputBookmarks.FindAll(x => x.Level == 1).Find(y => y.Page == k + 1); if (inputLevelOneBook != null) { includePage = false; for (int m = 0; m < areas.Length; m++) { if (inputLevelOneBook.Text.Contains(areaDiscLocations[areas[m]][3])) { includePage = true; } } } if (includePage) { pdfDoc.AddPage(inputDocument.Pages[k]); //bookmarks outlines = pdfDoc.Outlines; //-main if (!mainBookmark) { outlines.Add("Legend", pdfDoc.Pages[intPageNo]); mainBookmark = true; } //-area outlines = outlines[outlines.Count - 1].Outlines; if (inputLevelOneBook != null) { outlines.Add(inputLevelOneBook.Text, pdfDoc.Pages[intPageNo]); } //-page bookmarksLayout pageBookmark = inputBookmarks.FindAll(x => x.Level == 2).Find(y => y.Page == k + 1); outlines = outlines[outlines.Count - 1].Outlines; if (pageBookmark != null) { outlines.Add(pageBookmark.Text, pdfDoc.Pages[intPageNo]); } //add to page count intPagesAdded++; intPageNo = intPagesAdded; } } //Add approach plates for (int i = 0; i < inputAirfields.Count(); i++) { for (int j = 0; j < inputAirfields[i].Plates.Count; j++) { inputDocument = CompatiblePdfReader.Open(inputAirfields[i].Plates[j][1]); for (int k = 0; k < inputDocument.PageCount; k++) { pdfDoc.AddPage(inputDocument.Pages[k]); intPagesAdded++; } //add bookmarks outlines = pdfDoc.Outlines; if (j == 0) { //if start of doc or previous country not the same add a new country bookmark //absolute allows same statemant for first and all values of i, caught by i == 0 check if (i == 0 || inputAirfields[Math.Abs(i - 1)].Country != inputAirfields[i].Country) { outlines.Add(inputAirfields[i].Country, pdfDoc.Pages[intPageNo]); } outlines = outlines[outlines.Count - 1].Outlines; outlines.Add(inputAirfields[i].Name, pdfDoc.Pages[intPageNo]); } else { outlines = outlines[outlines.Count - 1].Outlines; } outlines = outlines[outlines.Count - 1].Outlines; outlines.Add(inputAirfields[i].Plates[j][0], pdfDoc.Pages[intPageNo]); intPageNo = intPagesAdded; } } //Save and close string areaFileName = ""; for (int m = 0; m < areas.Length; m++) { areaFileName += areaDiscLocations[areas[m]][4] + " "; } pdfDoc.Save(strOutputLoc + @"\" + areaFileName + "Terminals " + docVersion + ".pdf"); pdfDoc.Close(); }