Ejemplo n.º 1
0
        public void CanCreateSingleCustomPageSize()
        {
            var builder = new PdfDocumentBuilder();

            var page = builder.AddPage(120, 250);

            var font = builder.AddStandard14Font(Standard14Font.Helvetica);

            page.AddText("Small page.", 12, new PdfPoint(25, 200), font);

            var bytes = builder.Build();

            WriteFile(nameof(CanCreateSingleCustomPageSize), bytes);

            using (var document = PdfDocument.Open(bytes, ParsingOptions.LenientParsingOff))
            {
                Assert.Equal(1, document.NumberOfPages);

                var page1 = document.GetPage(1);

                Assert.Equal(120, page1.Width);
                Assert.Equal(250, page1.Height);

                Assert.Equal("Small page.", page1.Text);
            }
        }
Ejemplo n.º 2
0
        public void CanWriteSinglePageMixedRenderingMode()
        {
            var builder = new PdfDocumentBuilder();

            PdfPageBuilder page = builder.AddPage(PageSize.A4);

            PdfDocumentBuilder.AddedFont font = builder.AddStandard14Font(Standard14Font.Helvetica);

            page.AddText("Hello World!", 12, new PdfPoint(25, 520), font);

            page.SetTextRenderingMode(TextRenderingMode.Neither);

            page.AddText("Invisible!", 12, new PdfPoint(25, 500), font);

            page.SetTextRenderingMode(TextRenderingMode.Fill);

            page.AddText("Filled again!", 12, new PdfPoint(25, 480), font);

            var b = builder.Build();

            WriteFile(nameof(CanWriteSinglePageMixedRenderingMode), b);

            using (var document = PdfDocument.Open(b))
            {
                var page1 = document.GetPage(1);

                Assert.Equal(new[] { "Hello", "World!", "Invisible!", "Filled", "again!" }, page1.GetWords().Select(x => x.Text));
            }
        }
Ejemplo n.º 3
0
        private void writePDFOutput()
        {
            PdfPageBuilder page = builder.AddPage(PageSize.A4);

            // Fonts must be registered with the document builder prior to use to prevent duplication.
            PdfDocumentBuilder.AddedFont font = builder.AddStandard14Font(Standard14Font.Helvetica);

            //setting starting poin to write from.
            PdfPoint point = new PdfPoint(25, 700);

            //page.AddText(TreatedText, 12, point, font);
            TreatedText      = TreatedText.Replace(":", ".");
            TreatedTextArray = TreatedText.Split(". ");

            page.AddText($"This is a Placeholder! {TreatedTextArray[0]}  This is a placeholder.", 12, point, font);
            point.MoveY(14);
            //for (int line = 0; line < TreatedTextArray.Length; line++)
            //{
            //    try { page.AddText(TreatedTextArray[line], 12, point, font); }
            //    catch (InvalidOperationException)
            //    {

            //    }
            //    point.MoveY(14);
            //}



            byte[] documentBytes = builder.Build();

            File.WriteAllBytes(_outputPath + _myFileType, documentBytes);
        }
Ejemplo n.º 4
0
        public void CanWrite2PagesSharingJpeg()
        {
            var builder = new PdfDocumentBuilder();
            var page    = builder.AddPage(PageSize.A4);

            var font = builder.AddStandard14Font(Standard14Font.Helvetica);

            page.AddText("Smile", 12, new PdfPoint(25, page.PageSize.Height - 52), font);

            var img = IntegrationHelpers.GetDocumentPath("smile-250-by-160.jpg", false);

            var expectedBounds1 = new PdfRectangle(25, page.PageSize.Height - 300, 200, page.PageSize.Height - 200);

            var imageBytes = File.ReadAllBytes(img);

            var expectedBounds2 = new PdfRectangle(25, 600, 75, 650);

            var jpeg = page.AddJpeg(imageBytes, expectedBounds1);

            page.AddJpeg(jpeg, expectedBounds2);

            var expectedBounds3 = new PdfRectangle(30, 500, 130, 550);

            var page2 = builder.AddPage(PageSize.A4);

            page2.AddJpeg(jpeg, expectedBounds3);

            var bytes = builder.Build();

            WriteFile(nameof(CanWrite2PagesSharingJpeg), bytes);

            using (var document = PdfDocument.Open(bytes))
            {
                var page1 = document.GetPage(1);

                Assert.Equal("Smile", page1.Text);

                var page1Images = page1.GetImages().ToList();
                Assert.Equal(2, page1Images.Count);

                var image1 = page1Images[0];
                Assert.Equal(expectedBounds1, image1.Bounds);

                var image2 = page1Images[1];
                Assert.Equal(expectedBounds2, image2.Bounds);

                var page2Doc = document.GetPage(2);

                var image3 = Assert.Single(page2Doc.GetImages());

                Assert.NotNull(image3);

                Assert.Equal(expectedBounds3, image3.Bounds);

                Assert.Equal(imageBytes, image1.RawBytes);
                Assert.Equal(imageBytes, image2.RawBytes);
                Assert.Equal(imageBytes, image3.RawBytes);
            }
        }
Ejemplo n.º 5
0
        private static ReadOnlySpan <byte> CreatePdfWithMetadata()
        {
            var builder = new PdfDocumentBuilder();
            var page    = builder.AddPage(PageSize.A4);
            var font    = builder.AddStandard14Font(Standard14Font.Helvetica);

            builder.DocumentInformation.Title    = "Sample title";
            builder.DocumentInformation.Author   = "John Doe";
            builder.DocumentInformation.Subject  = "Philosophy";
            builder.DocumentInformation.Keywords = "Philosophy; Eastern;";
            builder.IncludeDocumentInformation   = true;

            page.AddText(SampleText, 12, new PdfPoint(25, 700), font);

            return(builder.Build());
        }
Ejemplo n.º 6
0
        public void CanWriteSinglePageStandard14FontHelloWorld()
        {
            var builder = new PdfDocumentBuilder();

            PdfPageBuilder page = builder.AddPage(PageSize.A4);

            PdfDocumentBuilder.AddedFont font = builder.AddStandard14Font(Standard14Font.Helvetica);

            page.AddText("Hello World!", 12, new PdfPoint(25, 520), font);

            var b = builder.Build();

            WriteFile(nameof(CanWriteSinglePageStandard14FontHelloWorld), b);

            using (var document = PdfDocument.Open(b))
            {
                var page1 = document.GetPage(1);

                Assert.Equal(new[] { "Hello", "World!" }, page1.GetWords().Select(x => x.Text));
            }
        }
Ejemplo n.º 7
0
        public void CanWriteSinglePageWithPng()
        {
            var builder = new PdfDocumentBuilder();
            var page    = builder.AddPage(PageSize.A4);

            var font = builder.AddStandard14Font(Standard14Font.Helvetica);

            page.AddText("Piggy", 12, new PdfPoint(25, page.PageSize.Height - 52), font);

            var img = IntegrationHelpers.GetDocumentPath("pdfpig.png", false);

            var expectedBounds = new PdfRectangle(25, page.PageSize.Height - 300, 200, page.PageSize.Height - 200);

            var imageBytes = File.ReadAllBytes(img);

            page.AddPng(imageBytes, expectedBounds);

            var bytes = builder.Build();

            WriteFile(nameof(CanWriteSinglePageWithPng), bytes);

            using (var document = PdfDocument.Open(bytes))
            {
                var page1 = document.GetPage(1);

                Assert.Equal("Piggy", page1.Text);

                var image = Assert.Single(page1.GetImages());

                Assert.NotNull(image);

                Assert.Equal(expectedBounds.BottomLeft, image.Bounds.BottomLeft);
                Assert.Equal(expectedBounds.TopRight, image.Bounds.TopRight);

                Assert.True(image.TryGetPng(out var png));
                Assert.NotNull(png);

                WriteFile(nameof(CanWriteSinglePageWithPng) + "out", png, "png");
            }
        }