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

            builder.DocumentInformation.Title = "Hello Roboto!";

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

            var font = builder.AddTrueTypeFont(TrueTypeTestHelper.GetFileBytes("Roboto-Regular.ttf"));

            page.AddText("eé", 12, new PdfPoint(30, 520), font);

            Assert.NotEmpty(page.Operations);

            var b = builder.Build();

            WriteFile(nameof(CanWriteRobotoAccentedCharacters), b);

            Assert.NotEmpty(b);

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

                Assert.Equal("eé", page1.Text);
            }
        }
Ejemplo n.º 2
0
        public void CanGeneratePdfA2BFile()
        {
            var builder = new PdfDocumentBuilder
            {
                ArchiveStandard = PdfAStandard.A2B
            };

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

            var font = builder.AddTrueTypeFont(TrueTypeTestHelper.GetFileBytes("Roboto-Regular.ttf"));

            page.AddText("Howdy PDF/A-2B and welcome!", 10, new PdfPoint(25, 700), font);

            var bytes = builder.Build();

            WriteFile(nameof(CanGeneratePdfA2BFile), bytes);

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

                Assert.True(pdf.TryGetXmpMetadata(out var xmp));

                Assert.NotNull(xmp.GetXDocument());
            }
        }
Ejemplo n.º 3
0
        public void CanGeneratePdfAFile(PdfAStandard standard)
        {
            var builder = new PdfDocumentBuilder
            {
                ArchiveStandard = standard
            };

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

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

            page.AddJpeg(imgBytes, new PdfRectangle(50, 70, 150, 130));

            var font = builder.AddTrueTypeFont(TrueTypeTestHelper.GetFileBytes("Roboto-Regular.ttf"));

            page.AddText($"Howdy PDF/{standard}!", 10, new PdfPoint(25, 700), font);

            var bytes = builder.Build();

            WriteFile(nameof(CanGeneratePdfAFile) + standard, bytes);

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

                Assert.True(pdf.TryGetXmpMetadata(out var xmp));

                Assert.NotNull(xmp.GetXDocument());
            }
        }
Ejemplo n.º 4
0
        public void CanAddHelloWorldToSimplePage()
        {
            var path    = IntegrationHelpers.GetDocumentPath("Single Page Simple - from open office.pdf");
            var doc     = PdfDocument.Open(path);
            var builder = new PdfDocumentBuilder();

            var page = builder.AddPage(doc, 1);

            page.DrawLine(new PdfPoint(30, 520), new PdfPoint(360, 520));
            page.DrawLine(new PdfPoint(360, 520), new PdfPoint(360, 250));

            page.SetStrokeColor(250, 132, 131);
            page.DrawLine(new PdfPoint(25, 70), new PdfPoint(100, 70), 3);
            page.ResetColor();
            page.DrawRectangle(new PdfPoint(30, 200), 250, 100, 0.5m);
            page.DrawRectangle(new PdfPoint(30, 100), 250, 100, 0.5m);

            var file = TrueTypeTestHelper.GetFileBytes("Andada-Regular.ttf");

            var font = builder.AddTrueTypeFont(file);

            var letters = page.AddText("Hello World!", 12, new PdfPoint(30, 50), font);

            Assert.NotEmpty(page.CurrentStream.Operations);

            var b = builder.Build();

            WriteFile(nameof(CanAddHelloWorldToSimplePage), b);

            Assert.NotEmpty(b);

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

                Assert.Equal("I am a simple pdf.Hello World!", page1.Text);

                var h = page1.Letters[18];

                Assert.Equal("H", h.Value);
                Assert.Equal("Andada-Regular", h.FontName);

                var comparer      = new DoubleComparer(0.01);
                var pointComparer = new PointComparer(comparer);

                for (int i = 0; i < letters.Count; i++)
                {
                    var readerLetter = page1.Letters[i + 18];
                    var writerLetter = letters[i];

                    Assert.Equal(readerLetter.Value, writerLetter.Value);
                    Assert.Equal(readerLetter.Location, writerLetter.Location, pointComparer);
                    Assert.Equal(readerLetter.FontSize, writerLetter.FontSize, comparer);
                    Assert.Equal(readerLetter.GlyphRectangle.Width, writerLetter.GlyphRectangle.Width, comparer);
                    Assert.Equal(readerLetter.GlyphRectangle.Height, writerLetter.GlyphRectangle.Height, comparer);
                    Assert.Equal(readerLetter.GlyphRectangle.BottomLeft, writerLetter.GlyphRectangle.BottomLeft, pointComparer);
                }
            }
        }
Ejemplo n.º 5
0
        public void CanWriteSinglePageHelloWorld()
        {
            var builder = new PdfDocumentBuilder();

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

            page.DrawLine(new PdfPoint(30, 520), new PdfPoint(360, 520));
            page.DrawLine(new PdfPoint(360, 520), new PdfPoint(360, 250));

            page.SetStrokeColor(250, 132, 131);
            page.DrawLine(new PdfPoint(25, 70), new PdfPoint(100, 70), 3);
            page.ResetColor();
            page.DrawRectangle(new PdfPoint(30, 200), 250, 100, 0.5m);
            page.DrawRectangle(new PdfPoint(30, 100), 250, 100, 0.5m);

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "TrueType");
            var file = Path.Combine(path, "Andada-Regular.ttf");

            var font = builder.AddTrueTypeFont(File.ReadAllBytes(file));

            var letters = page.AddText("Hello World!", 12, new PdfPoint(30, 50), font);

            Assert.NotEmpty(page.Operations);

            var b = builder.Build();

            WriteFile(nameof(CanWriteSinglePageHelloWorld), b);

            Assert.NotEmpty(b);

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

                Assert.Equal("Hello World!", page1.Text);

                var h = page1.Letters[0];

                Assert.Equal("H", h.Value);
                Assert.Equal("Andada-Regular", h.FontName);

                for (int i = 0; i < page1.Letters.Count; i++)
                {
                    var readerLetter = page1.Letters[i];
                    var writerLetter = letters[i];

                    Assert.Equal(readerLetter.Value, writerLetter.Value);
                    Assert.Equal(readerLetter.Location, writerLetter.Location);
                    Assert.Equal(readerLetter.FontSize, writerLetter.FontSize);
                    Assert.Equal(readerLetter.GlyphRectangle.Width, writerLetter.GlyphRectangle.Width);
                    Assert.Equal(readerLetter.GlyphRectangle.Height, writerLetter.GlyphRectangle.Height);
                    Assert.Equal(readerLetter.GlyphRectangle.BottomLeft, writerLetter.GlyphRectangle.BottomLeft);
                }
            }
        }
Ejemplo n.º 6
0
        public void WindowsOnlyCanWriteSinglePageAccentedCharactersSystemFont()
        {
            var builder = new PdfDocumentBuilder();

            builder.DocumentInformation.Title = "Hello Windows!";

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

            var file = @"C:\Windows\Fonts\Calibri.ttf";

            if (!File.Exists(file))
            {
                return;
            }

            byte[] bytes;
            try
            {
                bytes = File.ReadAllBytes(file);
            }
            catch
            {
                return;
            }

            var font = builder.AddTrueTypeFont(bytes);

            page.AddText("eé", 12, new PdfPoint(30, 520), font);

            Assert.NotEmpty(page.Operations);

            var b = builder.Build();

            WriteFile(nameof(WindowsOnlyCanWriteSinglePageAccentedCharactersSystemFont), b);

            Assert.NotEmpty(b);

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

                Assert.Equal("eé", page1.Text);
            }
        }
Ejemplo n.º 7
0
        public void CanCopyPage()
        {
            byte[] b;
            {
                var builder = new PdfDocumentBuilder();

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

                var file = TrueTypeTestHelper.GetFileBytes("Andada-Regular.ttf");

                var font = builder.AddTrueTypeFont(file);

                page1.AddText("Hello", 12, new PdfPoint(30, 50), font);

                Assert.NotEmpty(page1.CurrentStream.Operations);


                using (var readDocument = PdfDocument.Open(IntegrationHelpers.GetDocumentPath("bold-italic.pdf")))
                {
                    var rpage = readDocument.GetPage(1);

                    var page2 = builder.AddPage(PageSize.A4);
                    page2.CopyFrom(rpage);
                }

                b = builder.Build();
                Assert.NotEmpty(b);
            }

            WriteFile(nameof(CanCopyPage), b);

            using (var document = PdfDocument.Open(b))
            {
                Assert.Equal(2, document.NumberOfPages);

                var page1 = document.GetPage(1);

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

                var page2 = document.GetPage(2);

                Assert.Equal("Lorem ipsum dolor sit amet, consectetur adipiscing elit. ", page2.Text);
            }
        }
Ejemplo n.º 8
0
        public void CanWriteSinglePageHelloWorld()
        {
            var builder = new PdfDocumentBuilder();

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

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "TrueType");
            var file = Path.Combine(path, "Andada-Regular.ttf");

            var font = builder.AddTrueTypeFont(File.ReadAllBytes(file));

            page.AddText("Hello World!", 12, new PdfPoint(30, 50), font);

            Assert.NotEmpty(page.Operations);

            var b = builder.Build();

            Assert.NotEmpty(b);
        }
Ejemplo n.º 9
0
        public void CanCreateDocumentInformationDictionaryWithNonAsciiCharacters()
        {
            const string littlePig = "маленький поросенок";
            var          builder   = new PdfDocumentBuilder();

            builder.DocumentInformation.Title = littlePig;
            var page = builder.AddPage(PageSize.A4);
            var font = builder.AddTrueTypeFont(TrueTypeTestHelper.GetFileBytes("Roboto-Regular.ttf"));

            page.AddText(littlePig, 12, new PdfPoint(120, 600), font);

            var file = builder.Build();

            WriteFile(nameof(CanCreateDocumentInformationDictionaryWithNonAsciiCharacters), file);
            using (var document = PdfDocument.Open(file))
            {
                Assert.Equal(littlePig, document.Information.Title);
            }
        }
Ejemplo n.º 10
0
        public void CanWriteSinglePageWithCzechCharacters()
        {
            var builder = new PdfDocumentBuilder();
            var page    = builder.AddPage(PageSize.A4);

            var font = builder.AddTrueTypeFont(TrueTypeTestHelper.GetFileBytes("Roboto-Regular.ttf"));

            page.AddText("Hello: řó", 9,
                         new PdfPoint(30, page.PageSize.Height - 50), font);

            var bytes = builder.Build();

            WriteFile(nameof(CanWriteSinglePageWithCzechCharacters), bytes);

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

                Assert.Equal("Hello: řó", page1.Text);
            }
        }
Ejemplo n.º 11
0
        public void CanWriteTwoPageDocument()
        {
            var builder = new PdfDocumentBuilder();
            var page1   = builder.AddPage(PageSize.A4);
            var page2   = builder.AddPage(PageSize.A4);

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "TrueType");
            var file = Path.Combine(path, "Roboto-Regular.ttf");

            var font = builder.AddTrueTypeFont(File.ReadAllBytes(file));

            var topLine = new PdfPoint(30, page1.PageSize.Height - 60);
            var letters = page1.AddText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor", 9, topLine, font);

            page1.AddText("incididunt ut labore et dolore magna aliqua.", 9, new PdfPoint(30, topLine.Y - letters.Max(x => x.GlyphRectangle.Height) - 5), font);

            var page2Letters = page2.AddText("The very hungry caterpillar ate all the apples in the garden.", 12, topLine, font);
            var left         = page2Letters[0].GlyphRectangle.Left;
            var bottom       = page2Letters.Min(x => x.GlyphRectangle.Bottom);
            var right        = page2Letters[page2Letters.Count - 1].GlyphRectangle.Right;
            var top          = page2Letters.Max(x => x.GlyphRectangle.Top);

            page2.SetStrokeColor(10, 250, 69);
            page2.DrawRectangle(new PdfPoint(left, bottom), right - left, top - bottom);

            var bytes = builder.Build();

            WriteFile(nameof(CanWriteTwoPageDocument), bytes);

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

                Assert.StartsWith("Lorem ipsum dolor sit", page1Out.Text);

                var page2Out = document.GetPage(2);

                Assert.StartsWith("The very hungry caterpillar", page2Out.Text);
            }
        }
Ejemplo n.º 12
0
        public void CanGeneratePageWithMultipleStream()
        {
            var builder = new PdfDocumentBuilder();

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

            var file = TrueTypeTestHelper.GetFileBytes("Andada-Regular.ttf");

            var font = builder.AddTrueTypeFont(file);

            var letters = page.AddText("Hello", 12, new PdfPoint(30, 50), font);

            Assert.NotEmpty(page.CurrentStream.Operations);

            page.NewContentStreamAfter();

            page.AddText("World!", 12, new PdfPoint(50, 50), font);

            Assert.NotEmpty(page.CurrentStream.Operations);


            var b = builder.Build();

            WriteFile(nameof(CanGeneratePageWithMultipleStream), b);

            Assert.NotEmpty(b);

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

                Assert.Equal("HelloWorld!", page1.Text);

                var h = page1.Letters[0];

                Assert.Equal("H", h.Value);
                Assert.Equal("Andada-Regular", h.FontName);
            }
        }
Ejemplo n.º 13
0
        public static void Run(string trueTypeFontPath, string jpgImagePath)
        {
            var builder = new PdfDocumentBuilder
            {
                ArchiveStandard = PdfAStandard.A2A
            };

            var font = builder.AddTrueTypeFont(File.ReadAllBytes(trueTypeFontPath));

            var page    = builder.AddPage(PageSize.A4);
            var pageTop = new PdfPoint(0, page.PageSize.Top);

            var letters = page.AddText("This is some text added to the output file near the top of the page.",
                                       12,
                                       pageTop.Translate(20, -25),
                                       font);

            var bottomOfText = letters.Min(x => x.GlyphRectangle.Bottom);

            var imagePlacement = new PdfRectangle(new PdfPoint(50, bottomOfText - 200),
                                                  new PdfPoint(150, bottomOfText));

            page.AddJpeg(File.ReadAllBytes(jpgImagePath), imagePlacement);

            var fileBytes = builder.Build();

            try
            {
                var location = AppDomain.CurrentDomain.BaseDirectory;
                var output   = Path.Combine(location, "outputOfPdfA2A.pdf");
                File.WriteAllBytes(output, fileBytes);
                Console.WriteLine($"File output to: {output}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to write output to file due to error: {ex}.");
            }
        }
Ejemplo n.º 14
0
        public void CanWriteSinglePageWithAccentedCharacters()
        {
            var builder = new PdfDocumentBuilder();
            var page    = builder.AddPage(PageSize.A4);

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "TrueType");
            var file = Path.Combine(path, "Roboto-Regular.ttf");

            var font = builder.AddTrueTypeFont(File.ReadAllBytes(file));

            page.AddText("é (lower case, upper case É).", 9,
                         new PdfPoint(30, page.PageSize.Height - 50), font);

            var bytes = builder.Build();

            WriteFile(nameof(CanWriteSinglePageWithAccentedCharacters), bytes);

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

                Assert.Equal("é (lower case, upper case É).", page1.Text);
            }
        }
Ejemplo n.º 15
0
        public void WindowsOnlyCanWriteSinglePageHelloWorldSystemFont()
        {
            var builder = new PdfDocumentBuilder();

            builder.DocumentInformation.Title = "Hello Windows!";

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

            var file = @"C:\Windows\Fonts\BASKVILL.TTF";

            if (!File.Exists(file))
            {
                return;
            }

            byte[] bytes;
            try
            {
                bytes = File.ReadAllBytes(file);
            }
            catch
            {
                return;
            }

            var font = builder.AddTrueTypeFont(bytes);

            var letters = page.AddText("Hello World!", 16, new PdfPoint(30, 520), font);

            page.AddText("This is some further text continuing to write", 12, new PdfPoint(30, 500), font);

            Assert.NotEmpty(page.Operations);

            var b = builder.Build();

            WriteFile(nameof(WindowsOnlyCanWriteSinglePageHelloWorldSystemFont), b);

            Assert.NotEmpty(b);

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

                Assert.StartsWith("Hello World!", page1.Text);

                var h = page1.Letters[0];

                Assert.Equal("H", h.Value);
                Assert.Equal("BaskOldFace", h.FontName);

                var comparer      = new DoubleComparer(0.01);
                var pointComparer = new PointComparer(comparer);

                for (int i = 0; i < letters.Count; i++)
                {
                    var readerLetter = page1.Letters[i];
                    var writerLetter = letters[i];

                    Assert.Equal(readerLetter.Value, writerLetter.Value);
                    Assert.Equal(readerLetter.Location, writerLetter.Location, pointComparer);
                    Assert.Equal(readerLetter.FontSize, writerLetter.FontSize, comparer);
                    Assert.Equal(readerLetter.GlyphRectangle.Width, writerLetter.GlyphRectangle.Width, comparer);
                    Assert.Equal(readerLetter.GlyphRectangle.Height, writerLetter.GlyphRectangle.Height, comparer);
                    Assert.Equal(readerLetter.GlyphRectangle.BottomLeft, writerLetter.GlyphRectangle.BottomLeft, pointComparer);
                }
            }
        }