Exemple #1
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream file1Input, Stream file2Input)
        {
            PdfFixedDocument document = new PdfFixedDocument();

            // The documents are merged by creating an empty PDF document and appending the file to it.
            // The outlines from the source file are also included in the merged file.
            document.AppendFile(file1Input);
            int count = document.Pages.Count;

            document.AppendFile(file2Input);

            // Create outlines that point to each merged file.
            PdfOutlineItem file1Outline = CreateOutline("First file", document.Pages[0]);

            document.Outline.Add(file1Outline);
            PdfOutlineItem file2Outline = CreateOutline("Second file", document.Pages[count]);

            document.Outline.Add(file2Outline);

            // Optionally we can add a new page at the beginning of the merged document.
            PdfPage page = new PdfPage();

            document.Pages.Insert(0, page);

            PdfOutlineItem blankPageOutline = CreateOutline("Blank page", page);

            document.Outline.Insert(0, blankPageOutline);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.documentappend.pdf") };
            return(output);
        }
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "Outline.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.PageMode = PdfPageMode.UseOutlines;

                pdf.AddPage();
                pdf.AddPage();

                for (int i = 0; i < pdf.PageCount; ++i)
                {
                    PdfCanvas canvas = pdf.Pages[i].Canvas;
                    canvas.DrawString(260, 50, "Page " + (i + 1).ToString());
                }

                PdfOutlineItem root                 = pdf.OutlineRoot;
                PdfOutlineItem outlineForPage1      = root.AddChild("Page 1", 0);
                PdfOutlineItem outlineForPage2      = root.AddChild("Page 2", 1);
                PdfOutlineItem childOutlineForPage3 = outlineForPage2.AddChild("Page 3", 2);

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
Exemple #3
0
        private static PdfOutlineItem CreateOutline(string title, PdfPage page)
        {
            PdfPageDirectDestination pageDestination = new PdfPageDirectDestination();

            pageDestination.Page = page;
            pageDestination.Top  = 0;
            pageDestination.Left = 0;
            // Inherit current zoom
            pageDestination.Zoom = 0;

            // Create a go to action to be executed when the outline is clicked,
            // the go to action goes to specified destination.
            PdfGoToAction gotoPage = new PdfGoToAction();

            gotoPage.Destination = pageDestination;

            // Create the outline in the table of contents
            PdfOutlineItem outline = new PdfOutlineItem();

            outline.Title       = title;
            outline.VisualStyle = PdfOutlineItemVisualStyle.Italic;
            outline.Action      = gotoPage;

            return(outline);
        }
Exemple #4
0
            public OutlineTreeViewItem(PdfOutlineItem outlineItem, IPdfViewerController controller)
                : this(controller)
            {
                this.Header       = outlineItem.title;
                this._destination = outlineItem.dest;
                this._id          = outlineItem.id;

                this.Expanded += ExpandEventHandler;
            }
Exemple #5
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run()
        {
            PdfFixedDocument document = new PdfFixedDocument();
            document.DisplayMode = PdfDisplayMode.UseOutlines;

            PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 216);
            PdfBrush blackBrush = new PdfBrush();
            for (int i = 0; i < 10; i++)
            {
                PdfPage page = document.Pages.Add();
                page.Graphics.DrawString((i + 1).ToString(), helvetica, blackBrush, 50, 50);
            }

            PdfOutlineItem root = new PdfOutlineItem();
            root.Title = "Contents";
            root.VisualStyle = PdfOutlineItemVisualStyle.Bold;
            root.Color = new PdfRgbColor(255, 0, 0);
            document.Outline.Add(root);

            for (int i = 0; i < document.Pages.Count; i++)
            {
                // Create a destination to target page.
                PdfPageDirectDestination pageDestination = new PdfPageDirectDestination();
                pageDestination.Page = document.Pages[i];
                pageDestination.Top = 0;
                pageDestination.Left = 0;
                // Inherit current zoom
                pageDestination.Zoom = 0;

                // Create a go to action to be executed when the outline is clicked,
                // the go to action goes to specified destination.
                PdfGoToAction gotoPage = new PdfGoToAction();
                gotoPage.Destination = pageDestination;

                // Create the outline in the table of contents
                PdfOutlineItem outline = new PdfOutlineItem();
                outline.Title = string.Format("Go to page {0}", i + 1);
                outline.VisualStyle = PdfOutlineItemVisualStyle.Italic;
                outline.Action = gotoPage;
                root.Items.Add(outline);
            }
            root.Expanded = true;

            // Create an outline that will launch a link in the browser.
            PdfUriAction uriAction = new PdfUriAction();
            uriAction.URI = "http://www.xfiniumsoft.com/";

            PdfOutlineItem webOutline = new PdfOutlineItem();
            webOutline.Title = "http://www.xfiniumsoft.com/";
            webOutline.Color = new PdfRgbColor(0, 0, 255);
            webOutline.Action = uriAction;
            document.Outline.Add(webOutline);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.outlines.pdf") };
            return output;
        }
Exemple #6
0
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "OutlineWithStyles.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.PageMode   = PdfPageMode.UseOutlines;
                pdf.PageLayout = PdfPageLayout.OneColumn;

                BuildTestOutline(pdf);

                PdfOutlineItem root = pdf.OutlineRoot;

                for (int i = 0; i < root.ChildCount; i++)
                {
                    PdfOutlineItem child = root.GetChild(i);

                    if (i % 2 == 0)
                    {
                        child.Bold = true;
                    }
                    else
                    {
                        child.Italic = true;
                    }

                    for (int j = 0; j < child.ChildCount; j++)
                    {
                        PdfOutlineItem childOfChild = child.GetChild(j);

                        if (j % 2 == 0)
                        {
                            childOfChild.Color = new PdfRgbColor(Color.DarkGreen);
                        }
                        else
                        {
                            childOfChild.Color = new PdfRgbColor(Color.BlueViolet);
                        }

                        childOfChild.Bold   = true;
                        childOfChild.Italic = true;
                    }
                }

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "OutlineWithStyles.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.PageMode   = PdfPageMode.UseOutlines;
                pdf.PageLayout = PdfPageLayout.OneColumn;

                BuildTestOutline(pdf);

                PdfOutlineItem root = pdf.OutlineRoot;

                for (int i = 0; i < root.ChildCount; i++)
                {
                    PdfOutlineItem child = root.GetChild(i);

                    if (i % 2 == 0)
                    {
                        child.Bold = true;
                    }
                    else
                    {
                        child.Italic = true;
                    }

                    for (int j = 0; j < child.ChildCount; j++)
                    {
                        PdfOutlineItem childOfChild = child.GetChild(j);

                        if (j % 2 == 0)
                        {
                            childOfChild.Color = new PdfRgbColor(0, 100, 0); // dark green
                        }
                        else
                        {
                            childOfChild.Color = new PdfRgbColor(138, 43, 226); // blue violet
                        }
                        childOfChild.Bold   = true;
                        childOfChild.Italic = true;
                    }
                }

                pdf.Save(pathToFile);
            }

            Console.WriteLine($"The output is located in {Environment.CurrentDirectory}");
        }
Exemple #8
0
        private static void BuildTestOutline(PdfDocument pdf)
        {
            PdfOutlineItem root       = pdf.OutlineRoot;
            PdfOutlineItem lastParent = null;

            PdfFont times     = pdf.AddFont(PdfBuiltInFont.TimesItalic);
            double  pageWidth = pdf.GetPage(0).Width;

            PdfPage page = pdf.GetPage(pdf.PageCount - 1);

            for (int i = 1; i < 30; i++)
            {
                if (i > 1)
                {
                    page = pdf.AddPage();
                }

                page.Canvas.Font     = times;
                page.Canvas.FontSize = 16;

                string titleFormat = string.Format("Page {0}", i);

                double textWidth = page.Canvas.GetTextWidth(titleFormat);
                page.Canvas.DrawString(new PdfPoint((pageWidth - textWidth) / 2, 100), titleFormat);

                PdfGoToAction action = pdf.CreateGoToPageAction(i - 1, 0);

                if (i == 1 || i == 10 || i == 20)
                {
                    lastParent = root.AddChild(titleFormat, action);
                }
                else
                {
                    lastParent.AddChild(titleFormat, action);
                }
            }
        }
Exemple #9
0
        public IList <PdfOutlineItem> GetOutlines(int parentId)
        {
            if (!isOpen)
            {
                throw new PdfNoFileOpenedException();
            }
            Logger.LogInfo("Getting outlines children of parent " + parentId);
            IntPtr pArray = IntPtr.Zero;
            int    count  = 0;

            if (!PdfViewerGetOutlineItems(documentHandle, parentId, ref pArray, ref count) || count <= 0)
            {
                return(null);
            }

            IntPtr pItem      = pArray;
            long   piItemSize = Marshal.SizeOf(new PIOutlineItem());
            IList <PdfOutlineItem> outlineItems = new List <PdfOutlineItem>();

            for (int i = 0; i < count; i++)
            {
                PIOutlineItem  piOutlineItem = (PIOutlineItem)Marshal.PtrToStructure(pItem, typeof(PIOutlineItem));
                PdfOutlineItem outlineItem   = new PdfOutlineItem();
                outlineItem.id          = piOutlineItem.id;
                outlineItem.level       = piOutlineItem.level;
                outlineItem.descendants = piOutlineItem.descendants;
                outlineItem.title       = piOutlineItem.title;
                outlineItem.dest        = new PdfDestination(piOutlineItem.pageNo, TDestination.eDestinationXYZ,
                                                             piOutlineItem.pt.x, piOutlineItem.pt.y, 0, 0, piOutlineItem.zoom);
                outlineItems.Add(outlineItem);
                pItem = new IntPtr(pItem.ToInt64() + piItemSize);
            }
            PdfViewerDisposeOutlineItems(pArray, count);
            Logger.LogInfo("Returning outlines children of parent " + parentId);
            return(outlineItems);
        }
Exemple #10
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run()
        {
            PdfFixedDocument document = new PdfFixedDocument();

            document.DisplayMode = PdfDisplayMode.UseOutlines;

            PdfStandardFont helvetica  = new PdfStandardFont(PdfStandardFontFace.Helvetica, 216);
            PdfBrush        blackBrush = new PdfBrush();

            for (int i = 0; i < 10; i++)
            {
                PdfPage page = document.Pages.Add();
                page.Graphics.DrawString((i + 1).ToString(), helvetica, blackBrush, 50, 50);
            }

            PdfOutlineItem root = new PdfOutlineItem();

            root.Title       = "Contents";
            root.VisualStyle = PdfOutlineItemVisualStyle.Bold;
            root.Color       = new PdfRgbColor(255, 0, 0);
            document.Outline.Add(root);

            for (int i = 0; i < document.Pages.Count; i++)
            {
                // Create a destination to target page.
                PdfPageDirectDestination pageDestination = new PdfPageDirectDestination();
                pageDestination.Page = document.Pages[i];
                pageDestination.Top  = 0;
                pageDestination.Left = 0;
                // Inherit current zoom
                pageDestination.Zoom = 0;

                // Create a go to action to be executed when the outline is clicked,
                // the go to action goes to specified destination.
                PdfGoToAction gotoPage = new PdfGoToAction();
                gotoPage.Destination = pageDestination;

                // Create the outline in the table of contents
                PdfOutlineItem outline = new PdfOutlineItem();
                outline.Title       = string.Format("Go to page {0}", i + 1);
                outline.VisualStyle = PdfOutlineItemVisualStyle.Italic;
                outline.Action      = gotoPage;
                root.Items.Add(outline);
            }
            root.Expanded = true;

            // Create an outline that will launch a link in the browser.
            PdfUriAction uriAction = new PdfUriAction();

            uriAction.URI = "http://www.xfiniumsoft.com/";

            PdfOutlineItem webOutline = new PdfOutlineItem();

            webOutline.Title  = "http://www.xfiniumsoft.com/";
            webOutline.Color  = new PdfRgbColor(0, 0, 255);
            webOutline.Action = uriAction;
            document.Outline.Add(webOutline);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.outlines.pdf") };
            return(output);
        }
Exemple #11
0
        /// <summary>
        /// Exports the inner.
        /// </summary>
        /// <param name="stream">The stream</param>
        /// <param name="doc">The doc.</param>
        /// <param name="saveAuto">if set to <c>true</c> [save auto].</param>
        /// <param name="checkEmpty">if set to <c>true</c> [check empty].</param>
        /// <param name="processSettings">if set to <c>true</c> [process settings].</param>
        /// <param name="otfu">The otfu.</param>
        /// <param name="frc">The FRC.</param>
        /// <param name="pageCount">The page count.</param>
        internal void ExportInner(Stream stream, PdfDocument doc, bool saveAuto, bool checkEmpty, bool processSettings, OpenTypeFontUtility otfu, GcReportContext frc, int pageCount)
        {
            bool flag2;

            doc             = doc ?? CreatePdfDocument();
            this.currentDoc = doc;
            if (processSettings)
            {
                this.settings.AppendTo(doc);
            }
            bool flag = false;

            if (otfu != null)
            {
                this.openTypeFontUtility     = otfu;
                this.openTypeFontUtility.Dpi = this.Dpi;
            }
            else
            {
                this.openTypeFontUtility = new OpenTypeFontUtility(GcReportContext.defaultFont, this.Dpi);
                flag = true;
                this.openTypeFontUtility.ExternalFont += this.ExternalFont;
            }
            if (frc != null)
            {
                this.context = frc;
            }
            else
            {
                GcReportContext context = new GcReportContext(this.report, this.Dpi, this.openTypeFontUtility)
                {
                    UnitsPerInch = 0x48
                };
                this.context = context;
                this.context.GeneratePageBlocks();
            }
            if (this.report.Watermark != null)
            {
                this.report.Watermark.Height = (int)this.context.PageRects.PageRectangle.Height;
                this.report.Watermark.Width  = (int)this.context.PageRects.PageRectangle.Width;
            }
            ExporterState state = new ExporterState(this.context)
            {
                BlackAndWhite = this.report.BlackAndWhite
            };
            int num = 0;

            foreach (List <GcPageBlock> list in this.context.Pages)
            {
                state.PageCount += list.Count;
                num              = Math.Max(list.Count, num);
            }
            if (pageCount >= 0)
            {
                state.PageCount = pageCount;
            }
            else
            {
                state.PageCount += this.report.FirstPageNumber - 1;
            }
            switch (this.context.Report.PageOrder)
            {
            case PrintPageOrder.Auto:
                flag2 = this.context.Pages.Count >= num;
                break;

            case PrintPageOrder.DownThenOver:
                flag2 = true;
                break;

            case PrintPageOrder.OverThenDown:
                flag2 = false;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            List <PageState> list2 = new List <PageState>();

            if (flag2)
            {
                for (int i = 0; i < num; i++)
                {
                    state.CurrentHPageNumber++;
                    state.CurrentVPageNumber = 0;
                    foreach (List <GcPageBlock> list3 in this.context.Pages)
                    {
                        if (list3.Count > i)
                        {
                            state.CurrentPageNumber++;
                            state.CurrentVPageNumber++;
                            list2.Add(new PageState(state.CurrentHPageNumber, state.CurrentVPageNumber, state.CurrentPageNumber, list3[i]));
                        }
                    }
                }
            }
            else
            {
                foreach (List <GcPageBlock> list4 in this.context.Pages)
                {
                    state.CurrentVPageNumber++;
                    state.CurrentHPageNumber = 0;
                    foreach (GcPageBlock block in list4)
                    {
                        state.CurrentPageNumber++;
                        state.CurrentHPageNumber++;
                        list2.Add(new PageState(state.CurrentHPageNumber, state.CurrentVPageNumber, state.CurrentPageNumber, block));
                    }
                }
            }
            if (list2.Count > 0)
            {
                if (string.IsNullOrEmpty(this.report.PageRange))
                {
                    foreach (PageState state2 in list2)
                    {
                        state.CurrentHPageNumber = state2.HPageIndex;
                        state.CurrentVPageNumber = state2.VPageIndex;
                        state.CurrentPageNumber  = (state2.PageIndex + this.report.FirstPageNumber) - 1;
                        this.ExportPage(state, doc, state2.PageBlock);
                    }
                }
                else
                {
                    foreach (int num3 in Utilities.GetPageRange(this.report.PageRange, list2.Count))
                    {
                        PageState state3 = list2[num3 - 1];
                        state.CurrentHPageNumber = state3.HPageIndex;
                        state.CurrentVPageNumber = state3.VPageIndex;
                        state.CurrentPageNumber  = (state3.PageIndex + this.report.FirstPageNumber) - 1;
                        this.ExportPage(state, doc, state3.PageBlock);
                    }
                }
            }
            if (checkEmpty && (doc.Pages.PageCount <= 0))
            {
                this.ExportEmptyPage(state, doc);
            }
            if (state.Bookmarks.Count > 0)
            {
                PdfOutlineItem item;
                if (doc.Outlines.Items.Count > 0)
                {
                    item = doc.Outlines.Items[0];
                }
                else
                {
                    item = new PdfOutlineItem(this.report.Bookmark, state.FirstPage);
                    doc.Outlines.Items.Add(item);
                }
                Dictionary <Bookmark, List <PdfOutlineItem> > dictionary = new Dictionary <Bookmark, List <PdfOutlineItem> >();
                foreach (ExporterState.BookmarkState state4 in state.Bookmarks)
                {
                    PdfOutlineItem item2 = new PdfOutlineItem(state4.Bookmark.Text, new PdfXYZDestination(state4.Page, (float)state4.Location.X, (float)state4.Location.Y, 0f));
                    if (!dictionary.ContainsKey(state4.Bookmark))
                    {
                        dictionary.Add(state4.Bookmark, new List <PdfOutlineItem>());
                    }
                    dictionary[state4.Bookmark].Add(item2);
                }
                foreach (KeyValuePair <Bookmark, List <PdfOutlineItem> > pair in dictionary)
                {
                    Bookmark bookmark           = pair.Key;
                    List <PdfOutlineItem> list6 = pair.Value;
                    if ((bookmark.Parent == null) || !dictionary.ContainsKey(bookmark.Parent))
                    {
                        foreach (PdfOutlineItem item3 in list6)
                        {
                            item.ChildItems.Add(item3);
                        }
                    }
                    else
                    {
                        List <PdfOutlineItem> list7 = dictionary[bookmark.Parent];
                        foreach (PdfOutlineItem item4 in list6)
                        {
                            PdfOutlineItem item5 = list7[0];
                            if (list7.Count > 1)
                            {
                                foreach (PdfOutlineItem item6 in list7)
                                {
                                    if (item6.Dest.TargetPage == item4.Dest.TargetPage)
                                    {
                                        item5 = item6;
                                        break;
                                    }
                                }
                            }
                            item5.ChildItems.Add(item4);
                        }
                    }
                }
            }
            if (processSettings)
            {
                if ((this.settings.DestinationType != DestinationType.Auto) || (doc.Pages.PageCount > 0))
                {
                    PdfArray array = new PdfArray();
                    if ((this.settings.OpenPageNumber <= doc.Pages.PageCount) && (doc.Pages.PageCount > 0))
                    {
                        array.Add(doc.Pages.Kids[this.settings.OpenPageNumber - 1]);
                    }
                    else
                    {
                        array.Add(doc.Pages.Kids[0]);
                    }
                    switch (this.settings.DestinationType)
                    {
                    case DestinationType.Auto:
                        break;

                    case DestinationType.FitPage:
                        array.Add(PdfName.Fit);
                        break;

                    case DestinationType.FitWidth:
                        array.Add(PdfName.FitH);
                        break;

                    case DestinationType.FitHeight:
                        array.Add(PdfName.FitV);
                        array.Add(PdfNumber.Zero);
                        break;

                    case DestinationType.FitBox:
                        array.Add(PdfName.FitB);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    doc.Add(PdfName.OpenAction, array);
                }
                if ((this.settings.DocumentAttachments.Count > 0) && (doc.Pages.PageCount > 0))
                {
                    PdfPage page = doc.Pages.Kids[doc.Pages.PageCount - 1] as PdfPage;
                    if (page != null)
                    {
                        foreach (DocumentAttachment attachment in this.settings.DocumentAttachments)
                        {
                            if ((!string.IsNullOrEmpty(attachment.Name) && (attachment.FileStreamInner != null)) && attachment.FileStreamInner.CanRead)
                            {
                                PdfFileAttachmentAnnotation annotation = new PdfFileAttachmentAnnotation {
                                    FileSpecification = { FileName = attachment.Name }
                                };
                                attachment.FileStreamInner.WriteTo((Stream)annotation.FileSpecification.FileStream.Data);
                                annotation.FileSpecification.FileStream.Size = attachment.FileStreamInner.Length;
                                annotation.FileSpecification.FileStream.MIME = attachment.ContentType;
                                annotation.ModifiedDate = DateTime.Now;
                                annotation.FileSpecification.FileStream.ModifyDate   = DateTime.Now;
                                annotation.FileSpecification.FileStream.CreationDate = DateTime.Now;
                                if (!string.IsNullOrEmpty(attachment.Description))
                                {
                                    annotation.Contents = attachment.Description;
                                }
                                if (attachment.Compress)
                                {
                                    annotation.FileSpecification.FileStream.Filters.Enqueue(PdfFilter.FlateFilter);
                                }
                                annotation.Flags     = PdfAnnotationBase.Flag.NoView | PdfAnnotationBase.Flag.Hidden | PdfAnnotationBase.Flag.Invisible;
                                annotation.Rectangle = new PdfRectangle(-100f, -100f, 1f, 1f);
                                page.Annotations.Add(annotation);
                            }
                        }
                    }
                }
            }
            if (saveAuto)
            {
                SavePdfDocment(doc, stream);
            }
            if (flag)
            {
                this.openTypeFontUtility.ExternalFont -= this.ExternalFont;
            }
        }