Represents a collection of outlines.
Inheritance: PdfObject, IEnumerable
Ejemplo n.º 1
0
        internal void AddOutline(int level, string title, PdfPage destinationPage)
        {
            if (level < 1 || destinationPage == null)
            {
                return;
            }

            PdfDocument document = destinationPage.Owner;

            if (document == null)
            {
                return;
            }

            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);
        }
Ejemplo n.º 2
0
        private PdfOutline AddOutline(PdfOutlineCollection outlines, string title, PdfPage destinationPage, XPoint position)
        {
            var outline = outlines.Add(title, destinationPage, true);

            outline.Left = position.X;
            outline.Top  = position.Y;
            return(outline);
        }
Ejemplo n.º 3
0
 public static void AddPdf(IEnumerable <Outline> outlines, PdfDocument doc, PdfOutlineCollection pdfOutlines)
 {
     foreach (var outline in outlines)
     {
         var pageIndex = outline.Page;
         if (pageIndex != null)
         {
             var page       = doc.Pages[pageIndex.Value];
             var pdfOutline = new PdfOutline(outline.Content, page);
             pdfOutlines.Add(pdfOutline);
             AddPdf(outline.Children, doc, pdfOutline.Outlines);
         }
     }
 }
Ejemplo n.º 4
0
        private void CreateOutlines(PdfOutlineCollection outlineCollection, IList <HtmlModel> htmlModels, IDictionary <string, PartialPdfModel> pdfPages)
        {
            if (htmlModels?.Count > 0)
            {
                foreach (var htmlModel in htmlModels)
                {
                    PdfOutline outline = new PdfOutline()
                    {
                        Title  = htmlModel.Title,
                        Opened = true
                    };

                    if (!string.IsNullOrEmpty(htmlModel.ExternalLink))
                    {
                        outline.Elements.Add("/Type", new PdfString("/Action"));
                        outline.Elements.Add("/Subtype", new PdfString("/Link"));
                        outline.Elements.Add("/A", new PdfLiteral($"<</S/URI/URI({htmlModel.ExternalLink})>>"));
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(htmlModel.HtmlFilePath))
                        {
                            string filePath = GetFilePath(htmlModel.HtmlFilePath);

                            if (pdfPages.ContainsKey(filePath))
                            {
                                PartialPdfModel pdfModel = pdfPages[filePath];

                                if (!pdfModel.PageNumber.HasValue)
                                {
                                    pdfModel.PageNumber    = _currentNumberOfPages - 1;
                                    _currentNumberOfPages += pdfModel.NumberOfPages;
                                }

                                outline.DestinationPage     = outlineCollection.Owner.Pages[pdfModel.PageNumber.Value];
                                outline.PageDestinationType = PdfPageDestinationType.FitH;
                            }
                        }
                    }

                    outlineCollection.Add(outline);
                    CreateOutlines(outline.Outlines, htmlModel.Children, pdfPages);
                }
            }
        }
Ejemplo n.º 5
0
        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("Accept: text/html, application/xhtml+xml, */*");
                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 {
                    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
                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("*****@*****.**", "XX");
                    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);
        }