示例#1
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());
            }
        }
示例#2
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.CurrentStream.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);
            }
        }
示例#3
0
        public void WritesSameTableAsRead(string fontFile)
        {
            var fontBytes = TrueTypeTestHelper.GetFileBytes(fontFile);

            var parsed = TrueTypeFontParser.Parse(new TrueTypeDataBytes(new ByteArrayInputBytes(fontBytes)));

            var os2       = parsed.TableRegister.Os2Table;
            var os2Header = parsed.TableHeaders.Single(x => x.Value.Tag == TrueTypeHeaderTable.Os2);

            var os2InputBytes = fontBytes.Skip((int)os2Header.Value.Offset).Take((int)os2Header.Value.Length).ToArray();

            using (var stream = new MemoryStream())
            {
                os2.Write(stream);

                var result = stream.ToArray();

                Assert.Equal(os2InputBytes.Length, result.Length);

                for (var i = 0; i < os2InputBytes.Length; i++)
                {
                    var expected = os2InputBytes[i];
                    var actual   = result[i];

                    Assert.Equal(expected, actual);
                }
            }
        }
示例#4
0
        public void ParseAndadaRegular()
        {
            var bytes = TrueTypeTestHelper.GetFileBytes("Andada-Regular");

            var input = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            var font = TrueTypeFontParser.Parse(input);

            var name = font.Name;

            Assert.Equal("Andada Regular", name);

            Assert.Equal(1.001999, font.TableRegister.HeaderTable.Revision, new DoubleComparer(5));

            Assert.Equal(11, font.TableRegister.HeaderTable.Flags);

            Assert.Equal(1000, font.TableRegister.HeaderTable.UnitsPerEm);

            Assert.Equal(2011, font.TableRegister.HeaderTable.Created.Year);
            Assert.Equal(9, font.TableRegister.HeaderTable.Created.Month);
            Assert.Equal(30, font.TableRegister.HeaderTable.Created.Day);

            Assert.Equal(2017, font.TableRegister.HeaderTable.Modified.Year);
            Assert.Equal(5, font.TableRegister.HeaderTable.Modified.Month);
            Assert.Equal(4, font.TableRegister.HeaderTable.Modified.Day);
        }
示例#5
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());
            }
        }
示例#6
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);
                }
            }
        }
示例#7
0
        public void ParseSimpleGoogleDocssGautmi()
        {
            var bytes = TrueTypeTestHelper.GetFileBytes("google-simple-doc");

            var input = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            var font = TrueTypeFontParser.Parse(input);

            Assert.NotNull(font.TableRegister.HeaderTable);
        }
示例#8
0
        public void ParsePMingLiU()
        {
            var bytes = TrueTypeTestHelper.GetFileBytes("PMingLiU");

            var input = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            var font = TrueTypeFontParser.Parse(input);

            Assert.NotNull(font);
        }
示例#9
0
        public void RobotoHeaderReadCorrectly()
        {
            var data = new[]
            {
                // key, offset, length, checksum
                "DSIG 158596 8 1",
                "GDEF 316 72 408950881",
                "GPOS 388 35744 355098641",
                "GSUB 36132 662 3357985284",
                "OS/2 36796 96 3097700805",
                "cmap 36892 1750 298470964",
                "cvt  156132 38 119085513",
                "fpgm 156172 2341 2494100564",
                "gasp 156124 8 16",
                "glyf 38644 88820 3302131736",
                "head 127464 54 346075833",
                "hhea 127520 36 217516755",
                "hmtx 127556 4148 1859679943",
                "kern 131704 12306 2002873469",
                "loca 144012 2076 77421448",
                "maxp 146088 32 89459325",
                "name 146120 830 44343214",
                "post 146952 9171 3638780613",
                "prep 158516 77 251381919"
            };

            var bytes = TrueTypeTestHelper.GetFileBytes("Roboto-Regular");

            var input = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            var font = TrueTypeFontParser.Parse(input);

            foreach (var s in data)
            {
                var parts = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                var name = parts[0];

                if (name == "cvt")
                {
                    name = "cvt ";
                }

                var match = font.TableHeaders[name];

                var offset   = long.Parse(parts[1], CultureInfo.InvariantCulture);
                var length   = long.Parse(parts[2], CultureInfo.InvariantCulture);
                var checksum = long.Parse(parts[3], CultureInfo.InvariantCulture);

                Assert.Equal(offset, match.Offset);
                Assert.Equal(length, match.Length);
                Assert.Equal(checksum, match.CheckSum);
            }
        }
示例#10
0
        public void ParseIssue258CorruptNameTable()
        {
            var bytes = TrueTypeTestHelper.GetFileBytes("issue-258-corrupt-name-table");

            var input = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            var font = TrueTypeFontParser.Parse(input);

            Assert.NotNull(font);
            Assert.NotNull(font.TableRegister.NameTable);
            Assert.NotEmpty(font.TableRegister.NameTable.NameRecords);
        }
示例#11
0
        public void ParseRegularRoboto()
        {
            var bytes = TrueTypeTestHelper.GetFileBytes("Roboto-Regular");

            var input = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            var font = TrueTypeFontParser.Parse(input);

            Assert.Equal(1, font.Version);

            Assert.Equal(1, font.TableRegister.HeaderTable.Version);
            Assert.Equal(1, font.TableRegister.HeaderTable.Revision);

            Assert.Equal(1142661421u, font.TableRegister.HeaderTable.CheckSumAdjustment);
            Assert.Equal(1594834165u, font.TableRegister.HeaderTable.MagicNumber);

            Assert.Equal(9, font.TableRegister.HeaderTable.Flags);

            Assert.Equal(2048, font.TableRegister.HeaderTable.UnitsPerEm);

            Assert.Equal(2008, font.TableRegister.HeaderTable.Created.Year);
            Assert.Equal(09, font.TableRegister.HeaderTable.Created.Month);
            Assert.Equal(12, font.TableRegister.HeaderTable.Created.Day);
            Assert.Equal(12, font.TableRegister.HeaderTable.Created.Hour);
            Assert.Equal(29, font.TableRegister.HeaderTable.Created.Minute);
            Assert.Equal(34, font.TableRegister.HeaderTable.Created.Second);

            Assert.Equal(2011, font.TableRegister.HeaderTable.Modified.Year);
            Assert.Equal(11, font.TableRegister.HeaderTable.Modified.Month);
            Assert.Equal(30, font.TableRegister.HeaderTable.Modified.Day);
            Assert.Equal(5, font.TableRegister.HeaderTable.Modified.Hour);
            Assert.Equal(13, font.TableRegister.HeaderTable.Modified.Minute);
            Assert.Equal(10, font.TableRegister.HeaderTable.Modified.Second);

            Assert.Equal(-980, font.TableRegister.HeaderTable.Bounds.Left);
            Assert.Equal(-555, font.TableRegister.HeaderTable.Bounds.Bottom);

            Assert.Equal(2396, font.TableRegister.HeaderTable.Bounds.Right);
            Assert.Equal(2163, font.TableRegister.HeaderTable.Bounds.Top);

            Assert.Equal(HeaderTable.HeaderMacStyle.None, font.TableRegister.HeaderTable.MacStyle);
            Assert.Equal(9, font.TableRegister.HeaderTable.LowestRecommendedPpem);

            Assert.Equal(HeaderTable.FontDirection.StronglyLeftToRightWithNeutrals, font.TableRegister.HeaderTable.FontDirectionHint);

            Assert.Equal(IndexToLocationTable.EntryFormat.Short, font.TableRegister.HeaderTable.IndexToLocFormat);
            Assert.Equal(0, font.TableRegister.HeaderTable.GlyphDataFormat);
        }
示例#12
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);
            }
        }
示例#13
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);
            }
        }
        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);
            }
        }
示例#15
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);
            }
        }
示例#16
0
        public void ReadsRobotoGlyphSizesCorrectly()
        {
            var regex = new Regex(@"\?: Width (?<width>\d+), Height: (?<height>\d+), Points: (?<points>\d+)");

            var bytes = TrueTypeTestHelper.GetFileBytes("Roboto-Regular");

            var input = new TrueTypeDataBytes(new ByteArrayInputBytes(bytes));

            var font = TrueTypeFontParser.Parse(input);

            var robotoGlyphs = Encoding.ASCII.GetString(TrueTypeTestHelper.GetFileBytes("Roboto-Regular.GlyphData.txt"));
            var lines        = robotoGlyphs.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);

            for (var i = 0; i < lines.Length; i++)
            {
                var match = regex.Match(lines[i]);

                var width  = double.Parse(match.Groups["width"].Value, CultureInfo.InvariantCulture);
                var height = double.Parse(match.Groups["height"].Value, CultureInfo.InvariantCulture);
                var points = int.Parse(match.Groups["points"].Value, CultureInfo.InvariantCulture);

                var glyph = font.TableRegister.GlyphTable.Glyphs[i];

                // Vendor data ignores the empty glyph bounds.
                if (width == 0 && height == 0)
                {
                    continue;
                }

                if (i != 30)
                {
                    Assert.Equal(width, glyph.Bounds.Width);
                }

                Assert.Equal(height, glyph.Bounds.Height);
                Assert.Equal(points, glyph.Points.Length);
            }
        }
        public void CanWriteTwoPageDocument()
        {
            var builder = new PdfDocumentBuilder();
            var page1   = builder.AddPage(PageSize.A4);
            var page2   = builder.AddPage(PageSize.A4);

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

            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         = (decimal)page2Letters[0].GlyphRectangle.Left;
            var bottom       = (decimal)page2Letters.Min(x => x.GlyphRectangle.Bottom);
            var right        = (decimal)page2Letters[page2Letters.Count - 1].GlyphRectangle.Right;
            var top          = (decimal)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);
            }
        }