static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\..\\SupportFiles\\";

            FileStream       fs       = File.OpenRead(supportPath + "PDF4NET.pdf");
            PDFFixedDocument document = new PDFFixedDocument(fs);

            fs.Close();

            PDFPageRenderer pageRenderer = new PDFPageRenderer(document.Pages[0]);

            PDFRendererSettings settings = new PDFRendererSettings(144, 144);

            // Render only text and vector graphics, do not render images
            settings.RenderAnnotations = true;
            settings.RenderFormFields  = true;
            settings.RenderText        = true;
            settings.RenderGraphics    = true;
            settings.RenderImages      = false;
            settings.RenderingSurface  = pageRenderer.CreateRenderingSurface <PDFRgbRenderingSurface>(settings.DpiX, settings.DpiY);

            // Output will be a 24bit RGB PNG
            FileStream pngStream = File.Create("PDF4NET.Page.0.png");

            pageRenderer.ConvertPageToImage(pngStream, PDFPageImageFormat.Png, settings);
            pngStream.Flush();
            pngStream.Close();
        }
예제 #2
0
        static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\..\\SupportFiles\\";

            FileStream       fs       = File.OpenRead(supportPath + "PDF4NET.pdf");
            PDFFixedDocument document = new PDFFixedDocument(fs);

            fs.Close();

            PDFPageRenderer pageRenderer = new PDFPageRenderer(document.Pages[0]);

            PDFRendererSettings           settings  = new PDFRendererSettings(144, 144);
            PDFBlackWhiteRenderingSurface bwSurface = pageRenderer.CreateRenderingSurface <PDFBlackWhiteRenderingSurface>(settings.DpiX, settings.DpiY);

            // Use a simple threshold filter for conversion to B/W
            // Everything below 128 becomes black, everything above 128 becomes white
            bwSurface.BinarizationFilter = new PDFThresholdFilter(128);
            settings.RenderingSurface    = bwSurface;

            // Output will be a 1bit B/W PNG
            FileStream pngStream = File.Create("PDF4NET.Page.0.Dithering-Off.png");

            pageRenderer.ConvertPageToImage(pngStream, PDFPageImageFormat.Png, settings);
            pngStream.Flush();
            pngStream.Close();

            // Use a dithering filter for conversion to B/W
            bwSurface.BinarizationFilter = new PDFFloydSteinbergDitheringFilter();

            // Output will be a 1bit B/W PNG
            pngStream = File.Create("PDF4NET.Page.0.Dithering-On.png");
            pageRenderer.ConvertPageToImage(pngStream, PDFPageImageFormat.Png, settings);
            pngStream.Flush();
            pngStream.Close();
        }
예제 #3
0
        private static void CreateJavaScriptActions(PDFFixedDocument document, PDFFont font)
        {
            PDFPen   blackPen   = new PDFPen(new PDFRgbColor(0, 0, 0), 1);
            PDFBrush blackBrush = new PDFBrush();

            font.Size = 12;
            PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions();

            sao.Brush = blackBrush;
            sao.Font  = font;
            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.HorizontalAlign = PDFStringHorizontalAlign.Center;
            slo.VerticalAlign   = PDFStringVerticalAlign.Middle;

            for (int i = 0; i < document.Pages.Count; i++)
            {
                document.Pages[i].Canvas.DrawString("JavaScript actions:", font, blackBrush, 400, 480);

                document.Pages[i].Canvas.DrawRectangle(blackPen, 400, 500, 200, 20);
                slo.X = 500;
                slo.Y = 510;
                document.Pages[i].Canvas.DrawString("Click me", sao, slo);

                // Create a link annotation on top of the widget.
                PDFLinkAnnotation link = new PDFLinkAnnotation();
                document.Pages[i].Annotations.Add(link);
                link.DisplayRectangle = new PDFDisplayRectangle(400, 500, 200, 20);

                // Create a Javascript action and attach it to the link.
                PDFJavaScriptAction jsAction = new PDFJavaScriptAction();
                jsAction.Script = "app.alert({cMsg: \"JavaScript action: you are now on page " + (i + 1) + "\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});";
                link.Action     = jsAction;
            }
        }
예제 #4
0
        private static void CreateDocumentActions(PDFFixedDocument document)
        {
            // Create an action that will open the document at the last page with 200% zoom.
            PDFPageDirectDestination pageDestination = new PDFPageDirectDestination();

            pageDestination.Page = document.Pages[document.Pages.Count - 1];
            pageDestination.Zoom = 200;
            pageDestination.Top  = 0;
            pageDestination.Left = 0;
            PDFGoToAction openAction = new PDFGoToAction();

            openAction.Destination = pageDestination;
            document.OpenAction    = openAction;

            // Create an action that is executed when the document is closed.
            PDFJavaScriptAction jsCloseAction = new PDFJavaScriptAction();

            jsCloseAction.Script       = "app.alert({cMsg: \"The document will close now.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});";
            document.BeforeCloseAction = jsCloseAction;

            // Create an action that is executed before the document is printed.
            PDFJavaScriptAction jsBeforePrintAction = new PDFJavaScriptAction();

            jsBeforePrintAction.Script = "app.alert({cMsg: \"The document will be printed.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});";
            document.BeforePrintAction = jsBeforePrintAction;

            // Create an action that is executed after the document is printed.
            PDFJavaScriptAction jsAfterPrintAction = new PDFJavaScriptAction();

            jsAfterPrintAction.Script = "app.alert({cMsg: \"The document has been printed.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});";
            document.AfterPrintAction = jsAfterPrintAction;
        }
예제 #5
0
        private static void CreateLaunchActions(PDFFixedDocument document, PDFFont font)
        {
            PDFPen   blackPen   = new PDFPen(new PDFRgbColor(0, 0, 0), 1);
            PDFBrush blackBrush = new PDFBrush();

            font.Size = 12;
            PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions();

            sao.Brush = blackBrush;
            sao.Font  = font;
            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.HorizontalAlign = PDFStringHorizontalAlign.Center;
            slo.VerticalAlign   = PDFStringVerticalAlign.Middle;

            for (int i = 0; i < document.Pages.Count; i++)
            {
                document.Pages[i].Canvas.DrawString("Launch actions:", font, blackBrush, 400, 360);

                document.Pages[i].Canvas.DrawRectangle(blackPen, 400, 380, 200, 20);
                slo.X = 500;
                slo.Y = 390;
                document.Pages[i].Canvas.DrawString("Launch samples explorer", sao, slo);

                // Create a link annotation on top of the widget.
                PDFLinkAnnotation link = new PDFLinkAnnotation();
                document.Pages[i].Annotations.Add(link);
                link.DisplayRectangle = new PDFDisplayRectangle(400, 380, 200, 20);

                // Create a launch action and attach it to the link.
                PDFLaunchAction launchAction = new PDFLaunchAction();
                launchAction.FileName = "O2S.Components.PDF4NET.SamplesExplorer.Win.exe";
                link.Action           = launchAction;
            }
        }
예제 #6
0
        private static void CreateUriActions(PDFFixedDocument document, PDFFont font)
        {
            PDFPen   blackPen   = new PDFPen(new PDFRgbColor(0, 0, 0), 1);
            PDFBrush blackBrush = new PDFBrush();

            font.Size = 12;
            PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions();

            sao.Brush = blackBrush;
            sao.Font  = font;
            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.HorizontalAlign = PDFStringHorizontalAlign.Center;
            slo.VerticalAlign   = PDFStringVerticalAlign.Middle;

            for (int i = 0; i < document.Pages.Count; i++)
            {
                document.Pages[i].Canvas.DrawString("Uri actions:", font, blackBrush, 400, 420);

                document.Pages[i].Canvas.DrawRectangle(blackPen, 400, 440, 200, 20);
                slo.X = 500;
                slo.Y = 450;
                document.Pages[i].Canvas.DrawString("Go to o2sol.com", sao, slo);

                // Create a link annotation on top of the widget.
                PDFLinkAnnotation link = new PDFLinkAnnotation();
                document.Pages[i].Annotations.Add(link);
                link.DisplayRectangle = new PDFDisplayRectangle(400, 440, 200, 20);

                // Create an uri action and attach it to the link.
                PDFUriAction uriAction = new PDFUriAction();
                uriAction.URI = "http://www.o2sol.com";
                link.Action   = uriAction;
            }
        }
예제 #7
0
        static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\..\\SupportFiles\\";

            FileStream       fs       = File.OpenRead(supportPath + "PDF4NET.pdf");
            PDFFixedDocument document = new PDFFixedDocument(fs);

            fs.Close();

            PDFPageRenderer pageRenderer = new PDFPageRenderer(document.Pages[0]);

            PDFRendererSettings settings = new PDFRendererSettings(144, 144);

            // Set background to light gray
            // Transparency does not matter as the PDFRgbRenderingSurface does not support transparency
            settings.BackgroundColor  = 0x00D0D0D0;
            settings.RenderingSurface = pageRenderer.CreateRenderingSurface <PDFRgbRenderingSurface>(settings.DpiX, settings.DpiY);

            // Output will be a 24bit RGB PNG
            FileStream pngStream = File.Create("PDF4NET.Page.0.png");

            pageRenderer.ConvertPageToImage(pngStream, PDFPageImageFormat.Png, settings);
            pngStream.Flush();
            pngStream.Close();
        }
예제 #8
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream s1, Stream s2)
        {
            PDFFixedDocument document = new PDFFixedDocument();

            document.DisplayMode = PDFDisplayMode.UseAttachments;

            PDFStandardFont helvetica  = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16);
            PDFBrush        blackBrush = new PDFBrush();
            PDFPage         page       = document.Pages.Add();

            page.Canvas.DrawString("This document contains 2 file attachments:", helvetica, blackBrush, 50, 50);
            page.Canvas.DrawString("1. fileattachments.cs.html", helvetica, blackBrush, 50, 70);
            page.Canvas.DrawString("2. fileattachments.vb.html", helvetica, blackBrush, 50, 90);

            byte[] fileData1 = new byte[s1.Length];
            s1.Read(fileData1, 0, fileData1.Length);
            PDFDocumentFileAttachment fileAttachment1 = new PDFDocumentFileAttachment();

            fileAttachment1.Payload     = fileData1;
            fileAttachment1.FileName    = "fileattachments.cs.html";
            fileAttachment1.Description = "C# Source Code for FileAttachments sample";
            document.FileAttachments.Add(fileAttachment1);

            byte[] fileData2 = new byte[s2.Length];
            s2.Read(fileData2, 0, fileData2.Length);
            PDFDocumentFileAttachment fileAttachment2 = new PDFDocumentFileAttachment();

            fileAttachment2.Payload     = fileData1;
            fileAttachment2.FileName    = "fileattachments.vb.html";
            fileAttachment2.Description = "VB.NET Source Code for FileAttachments sample";
            document.FileAttachments.Add(fileAttachment2);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "fileattachments.pdf") };
            return(output);
        }
예제 #9
0
        public static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\SupportFiles\\";

            FileStream       input    = File.OpenRead(supportPath + "content.pdf");
            PDFFixedDocument document = new PDFFixedDocument(input);

            input.Close();

            PDFContentExtractor           ce            = new PDFContentExtractor(document.Pages[0]);
            PDFTextSearchResultCollection searchResults = ce.SearchText("lorem");

            if (searchResults.Count > 0)
            {
                PDFContentRedactor cr = new PDFContentRedactor(document.Pages[0]);

                cr.BeginRedaction();

                for (int i = 0; i < searchResults.Count; i++)
                {
                    cr.RedactArea(searchResults[i].VisualBounds);
                }

                cr.ApplyRedaction();
            }

            using (FileStream output = File.Create("RedactedSearchResults.pdf"))
            {
                document.Save(output);
            }
        }
예제 #10
0
        private static void CreateTextMarkupAnnotations(PDFFixedDocument document, PDFFont font)
        {
            PDFBrush blackBrush = new PDFBrush();

            PDFPage page = document.Pages.Add();

            page.Canvas.DrawString("Text markup annotations", font, blackBrush, 50, 50);

            PDFTextMarkupAnnotationType[] tmat = new PDFTextMarkupAnnotationType[]
            {
                PDFTextMarkupAnnotationType.Highlight, PDFTextMarkupAnnotationType.Squiggly, PDFTextMarkupAnnotationType.StrikeOut, PDFTextMarkupAnnotationType.Underline
            };

            PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions();

            sao.Brush = blackBrush;
            sao.Font  = font;

            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.HorizontalAlign = PDFStringHorizontalAlign.Center;
            slo.VerticalAlign   = PDFStringVerticalAlign.Bottom;
            for (int i = 0; i < tmat.Length; i++)
            {
                PDFTextMarkupAnnotation tma = new PDFTextMarkupAnnotation();
                page.Annotations.Add(tma);
                tma.DisplayRectangle = new PDFDisplayRectangle(50, 100 + 50 * i, 200, font.Size + 2);
                tma.TextMarkupType   = tmat[i];

                slo.X = 150;
                slo.Y = 100 + 50 * i + font.Size;

                page.Canvas.DrawString(tmat[i].ToString() + " annotation.", sao, slo);
            }
        }
예제 #11
0
        private static void CreateLineAnnotations(PDFFixedDocument document, PDFFont font)
        {
            PDFBrush blackBrush = new PDFBrush();

            PDFPage page = document.Pages.Add();

            page.Canvas.DrawString("Line annotations", font, blackBrush, 50, 50);

            PDFLineEndSymbol[] les = new PDFLineEndSymbol[]
            {
                PDFLineEndSymbol.Circle, PDFLineEndSymbol.ClosedArrow, PDFLineEndSymbol.None, PDFLineEndSymbol.OpenArrow
            };

            for (int i = 0; i < les.Length; i++)
            {
                PDFLineAnnotation la = new PDFLineAnnotation();
                page.Annotations.Add(la);
                la.Author        = "O2S.Components.PDF4NET";
                la.Contents      = "I am a line annotation with " + les[i].ToString() + " ending.";
                la.StartPoint    = new PDFPoint(50, 100 + 40 * i);
                la.EndPoint      = new PDFPoint(250, 100 + 40 * i);
                la.EndLineSymbol = les[i];
                page.Canvas.DrawString("Line end symbol: " + les[i].ToString(), font, blackBrush, 270, 100 + 40 * i);
            }
        }
예제 #12
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, "documentappend.pdf") };
            return(output);
        }
예제 #13
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run()
        {
            PDFFixedDocument document    = new PDFFixedDocument();
            PDFStandardFont  titleFont   = new PDFStandardFont(PDFStandardFontFace.HelveticaBold, 16);
            PDFStandardFont  barcodeFont = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);

            PDFPage page = document.Pages.Add();

            DrawGenericBarcodes(page, titleFont, barcodeFont);

            page = document.Pages.Add();
            DrawPharmaBarcodes(page, titleFont, barcodeFont);

            page = document.Pages.Add();
            DrawEanUpcBarcodes(page, titleFont, barcodeFont);

            page = document.Pages.Add();
            DrawPostAndTransportantionBarcodes(page, titleFont, barcodeFont);

            page = document.Pages.Add();
            Draw2DBarcodes(page, titleFont, barcodeFont);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "barcodes.pdf") };
            return(output);
        }
예제 #14
0
        public static void Main(string[] args)
        {
            // Extract the page content from the source file.
            FileStream     stream      = File.OpenRead("input.pdf");
            PDFFile        source      = new PDFFile(stream);
            PDFPageContent pageContent = source.ExtractPageContent(0);

            stream.Close();

            PDFFixedDocument document = new PDFFixedDocument();

            document.OptionalContentProperties = new PDFOptionalContentProperties();
            PDFPage page = document.Pages.Add();

            // Create an optional content group (layer) for the extracted page content.
            PDFOptionalContentGroup ocg = new PDFOptionalContentGroup();

            ocg.Name            = "Embedded page";
            ocg.VisibilityState = PDFOptionalContentGroupVisibilityState.AlwaysVisible;
            ocg.PrintState      = PDFOptionalContentGroupPrintState.NeverPrint;
            // Draw the extracted page content in the layer
            page.Canvas.BeginOptionalContentGroup(ocg);
            page.Canvas.DrawFormXObject(pageContent, 0, 0, page.Width, page.Height);
            page.Canvas.EndOptionalContentGroup();

            // Build the display tree for the optional content
            PDFOptionalContentDisplayTreeNode ocgNode = new PDFOptionalContentDisplayTreeNode(ocg);

            document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgNode);

            using (FileStream output = File.Create("EmbedPageAsLayer.pdf"))
            {
                document.Save(output);
            }
        }
예제 #15
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream input)
        {
            PDFFile file = new PDFFile(input);

            PDFPageContent[] content = file.ExtractPageContent(0, file.PageCount - 1);
            file = null;

            PDFFixedDocument document = new PDFFixedDocument();
            PDFPage          page1    = document.Pages.Add();

            // Draw the same page content 4 times on the new page, the content is scaled to half and flipped.
            page1.Canvas.DrawFormXObject(content[0],
                                         0, 0, page1.Width / 2, page1.Height / 2);
            page1.Canvas.DrawFormXObject(content[0],
                                         page1.Width / 2, 0, page1.Width / 2, page1.Height / 2, 0, PDFFlipDirection.VerticalFlip);
            page1.Canvas.DrawFormXObject(content[0],
                                         0, page1.Height / 2, page1.Width / 2, page1.Height / 2, 0, PDFFlipDirection.HorizontalFlip);
            page1.Canvas.DrawFormXObject(content[0],
                                         page1.Width / 2, page1.Height / 2, page1.Width / 2, page1.Height / 2,
                                         0, PDFFlipDirection.VerticalFlip | PDFFlipDirection.HorizontalFlip);

            PDFPage page2 = document.Pages.Add();

            // Draw 3 pages on the new page.
            page2.Canvas.DrawFormXObject(content[0],
                                         0, 0, page2.Width / 2, page2.Height / 2);
            page2.Canvas.DrawFormXObject(content[1],
                                         page2.Width / 2, 0, page2.Width / 2, page2.Height / 2);
            page2.Canvas.DrawFormXObject(content[2],
                                         0, page2.Height, page2.Height / 2, page2.Width, 90);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "pageimposition.pdf") };
            return(output);
        }
예제 #16
0
        /// <summary>
        /// Extracts text fragments from the 3rd page and highlights the glyphs in the fragment.
        /// </summary>
        /// <param name="document"></param>
        private static void ExtractImagesAndHighlight(PDFFixedDocument document)
        {
            PDFPen                     pen       = new PDFPen(new PDFRgbColor(255, 0, 192), 0.5);
            PDFBrush                   brush     = new PDFBrush(new PDFRgbColor(0, 0, 0));
            PDFStandardFont            helvetica = new PDFStandardFont(PDFStandardFontFace.Helvetica, 8);
            PDFStringAppearanceOptions sao       = new PDFStringAppearanceOptions();

            sao.Brush = brush;
            sao.Font  = helvetica;
            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.Width = 1000;

            PDFContentExtractor      ce  = new PDFContentExtractor(document.Pages[2]);
            PDFVisualImageCollection eic = ce.ExtractImages(false);

            for (int i = 0; i < eic.Count; i++)
            {
                string imageProperties = string.Format("Image ID: {0}\nPixel width: {1} pixels\nPixel height: {2} pixels\n" +
                                                       "Display width: {3} points\nDisplay height: {4} points\nHorizonal Resolution: {5} dpi\nVertical Resolution: {6} dpi",
                                                       eic[i].ImageID, eic[i].Width, eic[i].Height, eic[i].DisplayWidth, eic[i].DisplayHeight, eic[i].DpiX, eic[i].DpiY);

                PDFPath boundingPath = new PDFPath();
                boundingPath.StartSubpath(eic[i].ImageCorners[0].X, eic[i].ImageCorners[0].Y);
                boundingPath.AddLineTo(eic[i].ImageCorners[1].X, eic[i].ImageCorners[1].Y);
                boundingPath.AddLineTo(eic[i].ImageCorners[2].X, eic[i].ImageCorners[2].Y);
                boundingPath.AddLineTo(eic[i].ImageCorners[3].X, eic[i].ImageCorners[3].Y);
                boundingPath.CloseSubpath();

                document.Pages[2].Canvas.DrawPath(pen, boundingPath);
                slo.X = eic[i].ImageCorners[3].X + 1;
                slo.Y = eic[i].ImageCorners[3].Y + 1;
                document.Pages[2].Canvas.DrawString(imageProperties, sao, slo);
            }
        }
예제 #17
0
        /// <summary>
        /// Extracts text fragments from the 2nd page and highlights the glyphs in the fragment.
        /// </summary>
        /// <param name="document"></param>
        private static void ExtractTextAndHighlightGlyphs(PDFFixedDocument document)
        {
            PDFRgbColor penColor = new PDFRgbColor();
            PDFPen      pen      = new PDFPen(penColor, 0.5);
            Random      rnd      = new Random();

            byte[] rgb = new byte[3];

            PDFContentExtractor  ce  = new PDFContentExtractor(document.Pages[1]);
            PDFTextRunCollection trc = ce.ExtractTextRuns();
            PDFTextRun           tr  = trc[1];

            for (int i = 0; i < tr.Glyphs.Count; i++)
            {
                rnd.NextBytes(rgb);
                penColor.R = rgb[0];
                penColor.G = rgb[1];
                penColor.B = rgb[2];

                PDFPath boundingPath = new PDFPath();
                boundingPath.StartSubpath(tr.Glyphs[i].GlyphCorners[0].X, tr.Glyphs[i].GlyphCorners[0].Y);
                boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[1].X, tr.Glyphs[i].GlyphCorners[1].Y);
                boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[2].X, tr.Glyphs[i].GlyphCorners[2].Y);
                boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[3].X, tr.Glyphs[i].GlyphCorners[3].Y);
                boundingPath.CloseSubpath();

                document.Pages[1].Canvas.DrawPath(pen, boundingPath);
            }
        }
예제 #18
0
        /// <summary>
        /// Generates a PDF document that is encrypted using AES method.
        /// </summary>
        /// <param name="keySize">The encryption key size.</param>
        /// <param name="aes"></param>
        /// <returns></returns>
        private static PDFFixedDocument EncryptAES(int keySize, PDFAesSecurityHandler aes)
        {
            PDFFixedDocument doc = new PDFFixedDocument();

            aes.EnableContentExtractionForAccessibility = false;
            aes.EnableDocumentAssembly         = false;
            aes.EnableDocumentChange           = false;
            aes.EnableContentExtraction        = false;
            aes.EnableFormsFill                = false;
            aes.EnableAnnotationsAndFieldsEdit = false;
            aes.EnablePrint     = false;
            aes.EncryptMetadata = true;
            aes.KeySize         = keySize;
            aes.UserPassword    = "******";
            aes.OwnerPassword   = "******";

            PDFPage         page       = doc.Pages.Add();
            PDFStandardFont helvetica  = new PDFStandardFont(PDFStandardFontFace.HelveticaBoldItalic, 16);
            PDFBrush        blackBrush = new PDFBrush();

            string text = string.Format("Encryption: AES {0} bit", keySize);

            if ((aes.KeySize == 256) && aes.UseEnhancedPasswordValidation)
            {
                text = text + " with enhanced password validation (Acrobat X or later)";
            }
            page.Canvas.DrawString(text, helvetica, blackBrush, 50, 100);

            return(doc);
        }
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream input)
        {
            PDFDocumentFeatures df = new PDFDocumentFeatures();

            // Do not load file attachments, new file attachments cannot be added.
            df.EnableDocumentFileAttachments = false;
            // Do not load form fields, form fields cannot be filled and new form fields cannot be added.
            df.EnableDocumentFormFields = false;
            // Do not load embedded JavaScript code, new JavaScript code at document level cannot be added.
            df.EnableDocumentJavaScriptFragments = false;
            // Do not load the named destinations, new named destinations cannot be created.
            df.EnableDocumentNamedDestinations = false;
            // Do not load the document outlines, new outlines cannot be created.
            df.EnableDocumentOutline = false;
            // Do not load annotations, new annotations cannot be added to existing pages.
            df.EnablePageAnnotations = false;
            // Do not load the page graphics, new graphics cannot be added to existing pages.
            df.EnablePageGraphics = false;
            PDFFixedDocument document = new PDFFixedDocument(input, df);

            PDFStandardFont helvetica = new PDFStandardFont(PDFStandardFontFace.HelveticaBold, 24);
            PDFBrush        brush     = new PDFBrush();

            // Add a new page with some content on it.
            PDFPage page = document.Pages.Add();

            page.Canvas.DrawString("New page added to an existing document.", helvetica, brush, 20, 50);

            // When document features have been specified at load time the document is automatically saved in incremental update mode.
            document.Save(input);

            return(null);
        }
예제 #20
0
        public static void Main(string[] args)
        {
            PDFFixedDocument document  = new PDFFixedDocument();
            PDFPage          page      = document.Pages.Add();
            PDFPen           pen       = new PDFPen(PDFRgbColor.Black, 1);
            PDFStandardFont  helvetica = new PDFStandardFont(PDFStandardFontFace.Helvetica, 16);

            PDFPoint[] points = new PDFPoint[] {
                new PDFPoint(50, 150), new PDFPoint(100, 200), new PDFPoint(150, 50), new PDFPoint(200, 150), new PDFPoint(250, 50)
            };
            DrawBezierConnectedLines(page, points, pen, 0, helvetica);

            page.Canvas.TranslateTransform(0, 200);
            DrawBezierConnectedLines(page, points, pen, 0.1, helvetica);

            page.Canvas.TranslateTransform(0, 200);
            DrawBezierConnectedLines(page, points, pen, 0.3, helvetica);

            page.Canvas.TranslateTransform(0, 200);
            DrawBezierConnectedLines(page, points, pen, 0.5, helvetica);

            using (FileStream output = File.Create("BezierConnectedLines.pdf"))
            {
                document.Save(output);
            }
        }
예제 #21
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream input)
        {
            PDFRc4SecurityHandler rc4_40    = new PDFRc4SecurityHandler();
            PDFFixedDocument      document1 = EncryptRC4(40, rc4_40);
            PDFRc4SecurityHandler rc4_128   = new PDFRc4SecurityHandler();
            PDFFixedDocument      document2 = EncryptRC4(128, rc4_128);

            PDFAesSecurityHandler aes128    = new PDFAesSecurityHandler();
            PDFFixedDocument      document3 = EncryptAES(128, aes128);
            PDFAesSecurityHandler aes256    = new PDFAesSecurityHandler();
            PDFFixedDocument      document4 = EncryptAES(256, aes256);
            PDFAesSecurityHandler aes256e   = new PDFAesSecurityHandler();

            aes256e.UseEnhancedPasswordValidation = true;
            PDFFixedDocument document5 = EncryptAES(256, aes256e);
            PDFFixedDocument document6 = Decrypt(input);

            SampleOutputInfo[] output = new SampleOutputInfo[]
            {
                new SampleOutputInfo(document1, "encryption.rc4.40bit.pdf", rc4_40),
                new SampleOutputInfo(document2, "encryption.rc4.128bit.pdf", rc4_128),
                new SampleOutputInfo(document3, "encryption.aes.128bit.pdf", aes128),
                new SampleOutputInfo(document4, "encryption.aes.256bit.pdf", aes256),
                new SampleOutputInfo(document5, "encryption.aes.256bit.enhanced.pdf", aes256e),
                new SampleOutputInfo(document6, "encryption.decrypted.pdf"),
            };
            return(output);
        }
예제 #22
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream input)
        {
            PDFFixedDocument   document = new PDFFixedDocument(input);
            PDFContentRedactor crText   = new PDFContentRedactor(document.Pages[0]);

            // Redact a rectangular area of 200*100 points and leave the redacted area uncovered.
            crText.RedactArea(new PDFDisplayRectangle(50, 50, 200, 100));
            // Redact a rectangular area of 200*100 points and mark the redacted area with red.
            crText.RedactArea(new PDFDisplayRectangle(50, 350, 200, 100), PDFRgbColor.Red);

            PDFContentRedactor crImages = new PDFContentRedactor(document.Pages[2]);

            // Initialize the bulk redaction.
            crImages.BeginRedaction();
            // Prepare for redaction a rectangular area of 500*100 points and leave the redacted area uncovered.
            crImages.RedactArea(new PDFDisplayRectangle(50, 50, 500, 100));
            // Prepare for redaction a rectangular area of 200*100 points and mark the redacted area with red.
            crImages.RedactArea(new PDFDisplayRectangle(50, 350, 500, 100), PDFRgbColor.Red);
            // When images are redacted, the cleared pixels are set to 0. Depending on image colorspace the redacted area can appear black or colored.
            // Finish the redaction
            crImages.ApplyRedaction();

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "redaction.pdf") };
            return(output);
        }
예제 #23
0
        static void Main(string[] args)
        {
            // Load the PDF file.
            //PDF4NET v5: PDFDocument doc = new PDFDocument("..\\SupportFiles\\Images.pdf");
            PDFFixedDocument doc = new PDFFixedDocument("..\\..\\..\\..\\..\\SupportFiles\\content.pdf");

            //for (int i = 0; i < doc.Pages.Count; i++)
            //{
            // Convert the pages to PDFImportedPage to get access to ExtractImages method.
            //PDF4NET v5: PDFImportedPage ip = doc.Pages[i] as PDFImportedPage;
            PDFContentExtractor ce = new PDFContentExtractor(doc.Pages[2]);
            //PDF4NET v5: Bitmap[] images = ip.ExtractImages();
            PDFVisualImageCollection images = ce.ExtractImages(true);

            // Save the page images to disk, if there are any.
            for (int j = 0; j < images.Count; j++)
            {
                //PDF4NET v5: images[j].Save("image" + i.ToString() + j.ToString() + ".png", ImageFormat.Png);
                FileStream fs = File.OpenWrite("image" + j.ToString() + ".png");
                images[j].Save(fs, PDFVisualImageSaveFormat.Png);
                fs.Flush();
                fs.Close();
            }
            //}
        }
예제 #24
0
        static void Main(string[] args)
        {
            // Create the pdf document
            //PDF4NET v5: PDFDocument doc = new PDFDocument();
            PDFFixedDocument doc = new PDFFixedDocument();

            // Create a blank page
            //PDF4NET v5: PDFPage page = doc.AddPage();
            PDFPage page = doc.Pages.Add();

            // Create the signature field.
            PDFSignatureField sign = new PDFSignatureField("Signature");

            //PDF4NET v5: sign.Widgets[0].DisplayRectangle = new DisplayRectangle(50, 100, 200, 40);
            sign.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(50, 100, 200, 40);
            // Create the digital signature.
            //PDF4NET v5: sign.Signature = new PDFDigitalSignature();
            PDFCmsDigitalSignature ds = new PDFCmsDigitalSignature();

            // The certificate is loaded from disk, but it also can be loaded from a certificate store.
            ds.Certificate = new X509Certificate2("..\\..\\..\\..\\..\\SupportFiles\\O2SolutionsDemoCertificate.pfx", "P@ssw0rd!", X509KeyStorageFlags.Exportable);
            ds.Location    = "Romania";
            ds.Reason      = "For demo";
            ds.ContactInfo = "*****@*****.**";
            sign.Signature = ds;
            page.Fields.Add(sign);

            doc.Save("Sample_DigitalSign.pdf");
        }
예제 #25
0
        // For this sample to work, the following file
        // needs to exist: multicolumntextandimages.pdf.
        // Compile the corresponding sample to get this file
        public static void MultiPage()
        {
            //PDF4NET v5: PDFFile mctiFile = PDFFile.FromFile("Sample_MultiColumnTextAndImages-Secure.pdf", Encoding.ASCII.GetBytes("userpass"));
            FileStream stm  = File.OpenRead("..\\..\\..\\..\\..\\SupportFiles\\content.pdf");
            PDFFile    file = new PDFFile(stm);

            // extract the content of first 4 pages
            //PDF4NET v5: PDFImportedContent[] ic = mctiFile.ExtractPagesContent("0-3");
            PDFPageContent[] pc = file.ExtractPageContent(0, 3);

            // create the new document
            //PDF4NET v5: PDFDocument doc = new PDFDocument();
            PDFFixedDocument doc = new PDFFixedDocument();
            // add a page to it
            //PDF4NET v5: PDFPage page = doc.AddPage();
            PDFPage page = doc.Pages.Add();

            // draw the imported content (4 pages) on the new page
            //PDF4NET v5: page.Canvas.DrawImportedContent(ic[0], 0, 0, page.Width / 2, page.Height / 2);
            page.Canvas.DrawFormXObject(pc[0], 0, 0, page.Width / 2, page.Height / 2);
            //PDF4NET v5: page.Canvas.DrawImportedContent(ic[1], page.Width / 2, 0, page.Width / 2, page.Height / 2);
            page.Canvas.DrawFormXObject(pc[1], page.Width / 2, 0, page.Width / 2, page.Height / 2);
            //PDF4NET v5: page.Canvas.DrawImportedContent(ic[2], 0, page.Height / 2, page.Width / 2, page.Height / 2);
            page.Canvas.DrawFormXObject(pc[2], 0, page.Height / 2, page.Width / 2, page.Height / 2);
            //PDF4NET v5: page.Canvas.DrawImportedContent(ic[3], page.Width / 2, page.Height / 2, page.Width / 2, page.Height / 2);
            page.Canvas.DrawFormXObject(pc[3], page.Width / 2, page.Height / 2, page.Width / 2, page.Height / 2);

            // save the mixed document
            doc.Save("Sample_Imposition.pdf");

            //PDF4NET v5: mctiFile.Close();
            stm.Close();
        }
예제 #26
0
        private static void CreateInkAnnotations(PDFFixedDocument document, PDFFont font)
        {
            PDFBrush blackBrush = new PDFBrush();
            Random   rnd        = new Random();

            PDFPage page = document.Pages.Add();

            page.Canvas.DrawString("Ink annotations", font, blackBrush, 50, 50);

            // The ink annotation will contain 3 lines, each one with 10 points.
            PDFPoint[][] points = new PDFPoint[3][];
            for (int i = 0; i < points.Length; i++)
            {
                points[i] = new PDFPoint[10];
                for (int j = 0; j < points[i].Length; j++)
                {
                    points[i][j] = new PDFPoint(rnd.NextDouble() * page.Width, rnd.NextDouble() * page.Height);
                }
            }

            PDFInkAnnotation ia = new PDFInkAnnotation();

            page.Annotations.Add(ia);
            ia.Contents = "I am an ink annotation.";
            ia.InkColor = new PDFRgbColor(255, 0, 255);
            ia.InkWidth = 5;
            ia.Points   = points;
        }
예제 #27
0
        private static PDFFixedDocument CreatePDFA1bDocument(Stream iccInput, Stream ttfInput)
        {
            iccInput.Position = 0;
            ttfInput.Position = 0;

            PDFFixedDocument document = new PDFFixedDocument();

            // Setup the document by creating a PDF/A output intent, based on a RGB ICC profile,
            // and document information and metadata
            PDFIccColorSpace icc = new PDFIccColorSpace();

            byte[] profilePayload = new byte[iccInput.Length];
            iccInput.Read(profilePayload, 0, profilePayload.Length);
            icc.IccProfile = profilePayload;
            PDFOutputIntent oi = new PDFOutputIntent();

            oi.Type = PDFOutputIntentType.PDFA1;
            oi.Info = "sRGB IEC61966-2.1";
            oi.OutputConditionIdentifier = "Custom";
            oi.DestinationOutputProfile  = icc;
            document.OutputIntents       = new PDFOutputIntentCollection();
            document.OutputIntents.Add(oi);

            document.DocumentInformation          = new PDFDocumentInformation();
            document.DocumentInformation.Author   = "O2 Solutions";
            document.DocumentInformation.Title    = "PDF4NET PDF/A-1B Demo";
            document.DocumentInformation.Creator  = "PDF4NET PDF/A-1B Demo Application";
            document.DocumentInformation.Producer = "PDF4NET";
            document.DocumentInformation.Keywords = "pdf/a";
            document.DocumentInformation.Subject  = "PDF/A-1B Sample produced by PDF4NET";
            document.XmpMetadata = new PDFXmpMetadata();

            PDFPage page = document.Pages.Add();

            page.Rotation = 90;

            // All fonts must be embedded in a PDF/A document.
            PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions();

            sao.Font  = new PDFAnsiTrueTypeFont(ttfInput, 24, true);
            sao.Brush = new PDFBrush(new PDFRgbColor(0, 0, 128));

            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.HorizontalAlign = PDFStringHorizontalAlign.Center;
            slo.VerticalAlign   = PDFStringVerticalAlign.Bottom;
            slo.X = page.Width / 2;
            slo.Y = page.Height / 2 - 10;
            page.Canvas.DrawString("PDF4NET", sao, slo);
            slo.Y             = page.Height / 2 + 10;
            slo.VerticalAlign = PDFStringVerticalAlign.Top;
            sao.Font.Size     = 16;
            page.Canvas.DrawString("This is a sample PDF/A-1B document that shows the PDF/A-1B capabilities in PDF4NET library", sao, slo);

            return(document);
        }
예제 #28
0
        /// <summary>
        /// Decrypts a PDF file
        /// </summary>
        /// <param name="input">Input stream.</param>
        /// <returns></returns>
        private static PDFFixedDocument Decrypt(Stream input)
        {
            PDFFixedDocument doc = new PDFFixedDocument(input, "pdf4net");

            PDFPage         page       = doc.Pages[0];
            PDFStandardFont helvetica  = new PDFStandardFont(PDFStandardFontFace.HelveticaBoldItalic, 16);
            PDFBrush        blackBrush = new PDFBrush();

            page.Canvas.DrawString("Decrypted document", helvetica, blackBrush, 5, 5);

            return(doc);
        }
예제 #29
0
        static void Main(string[] args)
        {
            string supportPath = "..\\..\\..\\..\\..\\SupportFiles\\";

            PDFFixedDocument  document        = new PDFFixedDocument(supportPath + "PDF4NET.pdf");
            PDFSignatureField signature1Field = document.Form.Fields["Signature1"] as PDFSignatureField;

            PDFComputedDigitalSignature signature1 = signature1Field.Signature as PDFComputedDigitalSignature;

            Asn1Object[] asn1Signature = signature1.DecodeSignature();
            DumpSignature(asn1Signature[0], 0);
        }
예제 #30
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream input)
        {
            PDFFixedDocument document = new PDFFixedDocument();
            PDFPage          page     = document.Pages.Add();

            PDFSvgDrawing svg = new PDFSvgDrawing(input);

            page.Canvas.DrawFormXObject(svg, 20, 20, page.Width - 40, page.Width - 40);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "svgtopdf.pdf") };
            return(output);
        }