internal void AddOutline(int level, string title, PdfPage destinationPage) { if (level < 1 || destinationPage == null) { return; } PdfDocument document = destinationPage.Owner; if (document == null) { return; } PdfOutline.PdfOutlineCollection outlines = document.Outlines; while (--level > 0) { int count = outlines.Count; if (count == 0) { // You cannot add empty bookmarks to PDF. So we use blank here. PdfOutline outline = outlines.Add(" ", destinationPage, true); outlines = outline.Outlines; } else { outlines = outlines[count - 1].Outlines; } } outlines.Add(title, destinationPage, true); }
void AddOutlineCollection(Document document, PdfOutline.PdfOutlineCollection outlines, TreeIter parent) { if (outlines != null) { foreach (PdfOutline outline in outlines) { var iter = AddOutline(parent, outline); if (outline.Opened) { tree_view.ExpandRow(model.GetPath(iter), false); } // Recursively add this item's children, if any AddOutlineCollection(document, outline.Outlines, iter); } } }
public static PdfOutline CheckIfOutlineISPresent(string strBookMarkName, PdfOutline.PdfOutlineCollection outlineCollection) { string tempParent = ""; try { int i = strBookMarkName.LastIndexOf('\\'); if (i == -1) { return(null); } tempParent = strBookMarkName.Substring(0, i); if (tempParent == null) { return(null); } foreach (PdfOutline outline in outlineCollection) { if (outline.Title == tempParent) { return(outline); } else { PdfOutline outlineTemp = CheckIfOutlineISPresent(strBookMarkName, outline.Outlines); if (outlineTemp != null) { return(outlineTemp); } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } return(null); }
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(); }
private async void downloadCharts() { //get parameters bool blFileHDSelect = chkHighDef.Checked; string[] strSaveLocations = createSaveDirectories(txtSaveLocation.Text); lblFileDownloading.Text = "Collecting download information..."; List <chartInfo> lstChartInfo = getDownloadAddresses(); //create objects and vars int intTotalFiles = lstChartInfo.Count; PdfDocument doc = new PdfDocument(); int intPdfPageNo = 0; //download files and create PDF chartInfo previousChart = new chartInfo(); for (int i = 0; i < intTotalFiles; i++) { WebClient wc = new WebClient(); //set headers and security wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0"); ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //wc.Headers.Add("user-agent", "Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"); //set applicable charts chartInfo currentChart = lstChartInfo[i].setFileParameters(blFileHDSelect); //get file type string strFileType = Path.GetExtension(currentChart.FileLocation); string strFileSaveName = strSaveLocations[1] + "\\" + i.ToString(); //show file being downloaded string strFileInfo = currentChart.Name + " - " + currentChart.SubGroup + " - " + currentChart.Group + " "; int intIndexLengthRemove = 30; if (strFileInfo.Length < 31) { intIndexLengthRemove = strFileInfo.Length - 1; } lblFileDownloading.Text = strFileInfo.Remove(intIndexLengthRemove) + "..."; //download, if download fails move to next file try { await wc.DownloadFileTaskAsync(currentChart.FileLocation, strFileSaveName + strFileType); } catch (Exception e) { continue; } //process images to pdf int intPagesAdded = 0; if (strFileType == ".pdf") { //get downloaded pdf and add to output pdf PdfDocument inputDocument = PdfReader.Open(strFileSaveName + strFileType, PdfDocumentOpenMode.Import); foreach (PdfPage page in inputDocument.Pages) { page.Rotate = currentChart.FileRotation; doc.AddPage(page); intPagesAdded++; } } else { //turn downloaded image into pdf and add to output pdf PdfPage page = new PdfPage(); XImage img = XImage.FromFile(strFileSaveName + strFileType); page.Width = img.PointWidth; page.Height = img.PointHeight; doc.Pages.Add(page); XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[intPdfPageNo]); xgr.DrawImage(img, 0, 0); doc.Pages[intPdfPageNo].Rotate = currentChart.FileRotation; intPagesAdded++; } //add outline PdfOutline.PdfOutlineCollection outlines = doc.Outlines; //check whether new group level is required and add sub group outline if required if (currentChart.Group != previousChart.Group) { outlines.Add(currentChart.Group, doc.Pages[intPdfPageNo]); } if (currentChart.SubGroup.Length > 0) { //using count -1 gives the outline that was just created outlines = outlines[outlines.Count - 1].Outlines; if (currentChart.SubGroup != previousChart.SubGroup) { outlines.Add(currentChart.SubGroup, doc.Pages[intPdfPageNo]); } } //create and add final level outlines = outlines[outlines.Count - 1].Outlines; outlines.Add(lstChartInfo[i].Name, doc.Pages[intPdfPageNo]); //update page count intPdfPageNo += intPagesAdded; previousChart = currentChart; //give progress int intCompletedPercentage = Convert.ToInt16((((double)i + 1.0) / (double)intTotalFiles) * 100); pgrDownloadPerc.Value = intCompletedPercentage; } //save and close pdf doc.Save(strSaveLocations[2]); doc.Close(); //add clone with common name try { File.Copy(strSaveLocations[2], strSaveLocations[0] + @"\Met Charts.pdf", true); } catch { MessageBox.Show("Could not update the common file, ensure the file is not in use"); } //upload to cloud?? / email?? if (chkEmail.Checked) { try { lblFileDownloading.Text = "Sending email..."; MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("*****@*****.**"); mail.To.Add("*****@*****.**"); mail.Subject = "Met & NOTAMs"; mail.Body = "See weather charts attached"; mail.Attachments.Add(new Attachment(strSaveLocations[2])); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("*****@*****.**", "ginMONKEYS"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } //re-enable download button and complete message lblFileDownloading.Text = "Download complete."; enableControls(true); }
public static bool SaveTreeStructureToPDF(Dictionary <int, Dictionary <string, string> > bListFilePages, string strOutputPath) { bool bReturn = false; string strPreviousFile = "previous"; string strCurrentFile = "Current"; // Open the output document PdfDocument outputDocument = new PdfDocument(); PdfDocument inputDocument = new PdfDocument(); // Iterate files for (int iIndex = 0; iIndex < bListFilePages.Count; iIndex++) { try { Dictionary <string, string> innerDictionary = bListFilePages.ElementAt(iIndex).Value; //.Value.ToString(); string strBookMarkName = innerDictionary.ElementAt(0).Key.ToString(); string tempPath = innerDictionary.ElementAt(0).Value.ToString(); if (tempPath.Length > 0) { string strFilePath; string strPageNumber; PdfPage page = null; int iPageNumber = 0; int i = tempPath.IndexOf("#@$"); if (i != -1) { strFilePath = tempPath.Substring(0, i - 1); strPageNumber = tempPath.Substring(i + 3); strCurrentFile = strFilePath; if (strCurrentFile != strPreviousFile) { inputDocument = PdfReader.Open(strCurrentFile, PdfDocumentOpenMode.Import); } if (IsNumber(strPageNumber)) { if (strFilePath.Length > 0 && strPageNumber.Length > 0) { iPageNumber = Convert.ToInt16(strPageNumber); // ...and add it to the output document. page = outputDocument.AddPage(inputDocument.Pages[iPageNumber - 1]); } } strPreviousFile = strCurrentFile; } else { if (File.Exists(tempPath)) { inputDocument = PdfReader.Open(tempPath, PdfDocumentOpenMode.Import); page = outputDocument.AddPage(inputDocument.Pages[iPageNumber]); } else { page = GetFirstFilePageFromTree(iIndex, bListFilePages); page = outputDocument.AddPage(page); } } if (strBookMarkName.Length > 0) { // Create first page PdfOutline.PdfOutlineCollection outlineCollection = outputDocument.Outlines; PdfOutline outline = CheckIfOutlineISPresent(strBookMarkName, outlineCollection); if (outline != null) { outline.Outlines.Add(strBookMarkName, page, true, PdfOutlineStyle.Bold, PdfSharp.Drawing.XColors.Black); } else { // Create the root bookmark. You can set the style and the color. outputDocument.Outlines.Add(strBookMarkName, page, true, PdfOutlineStyle.Bold, PdfSharp.Drawing.XColors.Black); } bReturn = true; } } } catch (Exception ex) { } } try { if (bReturn) { outputDocument.Save(strOutputPath); } } catch (Exception ex) { bReturn = false; MessageBox.Show(ex.Message); } return(bReturn); }
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(); }