示例#1
0
        public virtual void CustomFontProvider()
        {
            String       fileName     = "customFontProvider.pdf";
            String       outFileName  = destinationFolder + fileName + ".pdf";
            String       cmpFileName  = sourceFolder + "cmp_" + fileName + ".pdf";
            FontProvider fontProvider = new FontProvider();

            // TODO DEVSIX-2119 Update if necessary
            fontProvider.GetFontSet().AddFont(StandardFonts.TIMES_ROMAN, null, "times");
            fontProvider.GetFontSet().AddFont(StandardFonts.HELVETICA);
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(fontProvider);
            Paragraph paragraph1 = new Paragraph("Default Helvetica should be selected.");

            doc.Add(paragraph1);
            Paragraph paragraph2 = new Paragraph("Default Helvetica should be selected.").SetFontFamily(StandardFonts.
                                                                                                        COURIER);

            doc.Add(paragraph2);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff" + fileName));
        }
示例#2
0
        public virtual void CustomFontWeight()
        {
            String       fileName    = "customFontWeight";
            String       outFileName = destinationFolder + fileName + ".pdf";
            String       cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
            FontProvider sel         = new FontProvider();

            sel.GetFontSet().AddFont(StandardFonts.HELVETICA);
            sel.GetFontSet().AddFont(StandardFonts.HELVETICA_BOLD);
            sel.GetFontSet().AddFont(StandardFonts.TIMES_ROMAN);
            // The provided alias is incorrect. It'll be used as a font's family, but since the name is invalid, the font shouldn't be selected
            sel.GetFontSet().AddFont(StandardFonts.TIMES_BOLD, null, "Times-Roman Bold");
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(sel);
            Div       div       = new Div().SetFontFamily(StandardFonts.TIMES_ROMAN);
            Paragraph paragraph = new Paragraph("Times Roman Bold text");

            paragraph.SetProperty(Property.FONT_WEIGHT, "bold");
            div.Add(paragraph);
            doc.Add(div);
            doc.Add(new Paragraph("UPD: The paragraph above should be written in Helvetica-Bold. The provided alias for Times-Bold was incorrect. It was used as a font's family, but since the name is invalid, the font wasn't selected."
                                  ));
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff" + fileName));
        }
示例#3
0
        public virtual void SingleFontWithUnicodeRange()
        {
            String fileName = "singleFontWithUnicodeRange";
            //In the result pdf will be two equal fonts but with different subsets
            String       outFileName = destinationFolder + fileName + ".pdf";
            String       cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
            FontProvider sel         = new FontProvider();

            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().AddFont(fontsFolder + "NotoSans-Regular.ttf", null, "FontAlias"
                                                                   ));
            NUnit.Framework.Assert.IsFalse(sel.GetFontSet().AddFont(fontsFolder + "NotoSans-Regular.ttf", null, "FontAlias"
                                                                    ));
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Size() == 1);
            String      s      = "Hello world! Здравствуй мир! Hello world! Здравствуй мир!";
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(sel);
            doc.SetProperty(Property.FONT, new String[] { "FontAlias" });
            Text      text      = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
            Paragraph paragraph = new Paragraph(text);

            doc.Add(paragraph);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff" + fileName));
        }
示例#4
0
        public virtual void CustomFontProvider2()
        {
            String       fileName     = "customFontProvider2.pdf";
            String       outFileName  = destinationFolder + fileName + ".pdf";
            String       cmpFileName  = sourceFolder + "cmp_" + fileName + ".pdf";
            FontProvider fontProvider = new FontProvider();

            // bold font. shouldn't be selected
            // TODO DEVSIX-2119 Update if necessary
            fontProvider.GetFontSet().AddFont(StandardFonts.TIMES_BOLD, null, "times");
            // monospace font. shouldn't be selected
            fontProvider.GetFontSet().AddFont(StandardFonts.COURIER);
            fontProvider.GetFontSet().AddFont(sourceFolder + "../fonts/FreeSans.ttf", PdfEncodings.IDENTITY_H);
            // TODO DEVSIX-2119 Update if necessary
            fontProvider.GetFontSet().AddFont(StandardFonts.TIMES_ROMAN, null, "times");
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(fontProvider);
            Paragraph paragraph = new Paragraph("There is no default font (Helvetica) inside the used FontProvider's instance. So the first font, that has been added, should be selected. Here it's FreeSans."
                                                ).SetFontFamily("ABRACADABRA_THERE_IS_NO_SUCH_FONT");

            doc.Add(paragraph);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff" + fileName));
        }
示例#5
0
        public virtual void CyrillicAndLatinWithUnicodeRange()
        {
            String       fileName    = "cyrillicAndLatinWithUnicodeRange";
            String       outFileName = destinationFolder + fileName + ".pdf";
            String       cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
            FontProvider sel         = new FontProvider();

            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().AddFont(fontsFolder + "NotoSans-Regular.ttf", null, "FontAlias"
                                                                   , new RangeBuilder(0, 255).Create()));
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().AddFont(fontsFolder + "FreeSans.ttf", null, "FontAlias", new
                                                                   RangeBuilder(1024, 1279).Create()));
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Size() == 2);
            String      s      = "Hello world! Здравствуй мир! Hello world! Здравствуй мир!";
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(sel);
            doc.SetProperty(Property.FONT, new String[] { "FontAlias" });
            Text      text      = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
            Paragraph paragraph = new Paragraph(text);

            doc.Add(paragraph);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff" + fileName));
        }
示例#6
0
        public virtual void CustomFontWeight3()
        {
            String       fileName    = "customFontWeight3";
            String       outFileName = destinationFolder + fileName + ".pdf";
            String       cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
            FontProvider sel         = new FontProvider();

            sel.GetFontSet().AddFont(StandardFonts.HELVETICA);
            sel.GetFontSet().AddFont(StandardFonts.HELVETICA_BOLD);
            sel.GetFontSet().AddFont(StandardFonts.TIMES_ROMAN);
            // correct alias
            sel.GetFontSet().AddFont(StandardFonts.TIMES_BOLD);
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(sel);
            Div       div       = new Div().SetFontFamily(StandardFontFamilies.TIMES);
            Paragraph paragraph = new Paragraph("Times Roman Bold text");

            paragraph.SetProperty(Property.FONT_WEIGHT, "bold");
            div.Add(paragraph);
            doc.Add(div);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff" + fileName));
        }
示例#7
0
        public virtual void NonBreakingHyphenDifferentFonts()
        {
            //TODO: update after fix of DEVSIX-2034
            String       outFileName = destinationFolder + "nonBreakingHyphenDifferentFonts.pdf";
            String       cmpFileName = sourceFolder + "cmp_nonBreakingHyphenDifferentFonts.pdf";
            String       diffPrefix  = "diff01_";
            Document     document    = new Document(new PdfDocument(new PdfWriter(outFileName)));
            FontProvider sel         = new FontProvider();

            sel.GetFontSet().AddFont(StandardFonts.TIMES_ROMAN);
            sel.GetFontSet().AddFont(StandardFonts.COURIER);
            sel.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H, "Puritan2");
            sel.GetFontSet().AddFont(fontsFolder + "NotoSans-Regular.ttf", PdfEncodings.IDENTITY_H, "NotoSans");
            document.SetFontProvider(sel);
            document.Add(new Paragraph("StandardFonts - non-breaking hyphen \\u2011").SetUnderline().SetTextAlignment(
                             TextAlignment.CENTER));
            document.Add(new Paragraph("for Standard font TIMES_ROMAN: <&#8209;> non-breaking hyphen <\u2011> 2 hyphens<\u2011\u2011>here "
                                       ).SetFont(StandardFonts.TIMES_ROMAN));
            document.Add(new Paragraph("for Standard font COURIER: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
                                       ).SetFont(StandardFonts.COURIER));
            document.Add(new Paragraph("for Standard font HELVETICA_BOLD: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
                                       ).SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD)));
            document.Add(new Paragraph("for Standard font SYMBOL: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
                                       ).SetFont(PdfFontFactory.CreateFont(StandardFonts.SYMBOL)));
            document.Add(new Paragraph("Non-Standard fonts - non-breaking hyphen \\u2011").SetUnderline().SetTextAlignment
                             (TextAlignment.CENTER));
            document.Add(new Paragraph("for NotoSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("NotoSans"
                                                                                                                     ));
            document.Add(new Paragraph("for Puritan2: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("Puritan2"
                                                                                                                     ));
            sel.GetFontSet().AddFont(fontsFolder + "FreeSans.ttf", PdfEncodings.IDENTITY_H, "FreeSans");
            document.Add(new Paragraph("AFTER adding of FreeSans font with non-breaking hyphen \\u2011 support").SetUnderline
                             ().SetTextAlignment(TextAlignment.CENTER));
            document.Add(new Paragraph("for Standard font TIMES_ROMAN: <&#8209;> non-breaking hyphen <\u2011> 2 hyphens<\u2011\u2011>here "
                                       ).SetFont(StandardFonts.TIMES_ROMAN));
            document.Add(new Paragraph("for Standard font COURIER: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
                                       ).SetFont(StandardFonts.COURIER));
            document.Add(new Paragraph("for Standard font HELVETICA_BOLD: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
                                       ).SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD)));
            document.Add(new Paragraph("for Standard font SYMBOL: <&#8209;> non-breaking hyphen<\u2011> 2hyphens <\u2011\u2011>here "
                                       ).SetFont(PdfFontFactory.CreateFont(StandardFonts.SYMBOL)));
            document.Add(new Paragraph("for FreeSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("FreeSans"
                                                                                                                     ));
            document.Add(new Paragraph("for NotoSans: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("NotoSans"
                                                                                                                     ));
            document.Add(new Paragraph("for Puritan2: <&#8209;> hyphen<\u2011> 2hyphens <\u2011\u2011>here").SetFont("Puritan2"
                                                                                                                     ));
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , diffPrefix));
        }
示例#8
0
        public virtual void TestReusingFontProvider()
        {
            String       testName     = "testReusingFontProvider";
            String       path         = PdfHelper.GetDefaultImagePath();
            String       pdfPathA3u   = PdfHelper.GetTargetDirectory() + testName + "_a3u.pdf";
            String       pdfPath      = PdfHelper.GetTargetDirectory() + testName + ".pdf";
            FileInfo     file         = new FileInfo(path);
            FontProvider fontProvider = new FontProvider("FreeSans");

            fontProvider.AddFont(PdfHelper.GetFreeSansFontPath());
            PdfOcrFontProvider      pdfOcrFontProvider      = new PdfOcrFontProvider(fontProvider.GetFontSet(), "FreeSans");
            OcrPdfCreatorProperties ocrPdfCreatorProperties = new OcrPdfCreatorProperties();

            ocrPdfCreatorProperties.SetPdfLang("en-US");
            ocrPdfCreatorProperties.SetFontProvider(pdfOcrFontProvider);
            PdfHelper.CreatePdfA(pdfPathA3u, file, ocrPdfCreatorProperties, PdfHelper.GetCMYKPdfOutputIntent());
            PdfHelper.CreatePdf(pdfPath, file, ocrPdfCreatorProperties);
            ExtractionStrategy strategy = PdfHelper.GetExtractionStrategy(pdfPathA3u);
            PdfFont            font     = strategy.GetPdfFont();
            String             fontName = font.GetFontProgram().GetFontNames().GetFontName();

            NUnit.Framework.Assert.IsTrue(fontName.Contains("FreeSans"));
            NUnit.Framework.Assert.IsTrue(font.IsEmbedded());
            NUnit.Framework.Assert.AreEqual(PdfHelper.DEFAULT_TEXT, strategy.GetResultantText());
            strategy = PdfHelper.GetExtractionStrategy(pdfPath);
            font     = strategy.GetPdfFont();
            fontName = font.GetFontProgram().GetFontNames().GetFontName();
            NUnit.Framework.Assert.IsTrue(fontName.Contains("FreeSans"));
            NUnit.Framework.Assert.IsTrue(font.IsEmbedded());
            NUnit.Framework.Assert.AreEqual(PdfHelper.DEFAULT_TEXT, strategy.GetResultantText());
        }
        protected internal override void DoDraw(SvgDrawContext context)
        {
            if (this.attributesAndStyles != null && this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.TEXT_CONTENT
                                                                                         ))
            {
                PdfCanvas      currentCanvas    = context.GetCurrentCanvas();
                String         xRawValue        = this.attributesAndStyles.Get(SvgConstants.Attributes.X);
                String         yRawValue        = this.attributesAndStyles.Get(SvgConstants.Attributes.Y);
                String         fontSizeRawValue = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_SIZE);
                IList <String> xValuesList      = SvgCssUtils.SplitValueList(xRawValue);
                IList <String> yValuesList      = SvgCssUtils.SplitValueList(yRawValue);
                float          x        = 0f;
                float          y        = 0f;
                float          fontSize = 0f;
                if (fontSizeRawValue != null && !String.IsNullOrEmpty(fontSizeRawValue))
                {
                    fontSize = CssUtils.ParseAbsoluteLength(fontSizeRawValue, CommonCssConstants.PT);
                }
                if (!xValuesList.IsEmpty())
                {
                    x = CssUtils.ParseAbsoluteLength(xValuesList[0]);
                }
                if (!yValuesList.IsEmpty())
                {
                    y = CssUtils.ParseAbsoluteLength(yValuesList[0]);
                }
                currentCanvas.BeginText();
                FontProvider provider  = context.GetFontProvider();
                FontSet      tempFonts = context.GetTempFonts();
                PdfFont      font      = null;
                if (!provider.GetFontSet().IsEmpty() || (tempFonts != null && !tempFonts.IsEmpty()))
                {
                    String fontFamily = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_FAMILY);
                    String fontWeight = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_WEIGHT);
                    String fontStyle  = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_STYLE);
                    fontFamily = fontFamily != null?fontFamily.Trim() : "";

                    FontInfo fontInfo = ResolveFontName(fontFamily, fontWeight, fontStyle, provider, tempFonts);
                    font = provider.GetPdfFont(fontInfo, tempFonts);
                }
                if (font == null)
                {
                    try {
                        // TODO (DEVSIX-2057)
                        // TODO each call of createFont() create a new instance of PdfFont.
                        // TODO FontProvider shall be used instead.
                        font = PdfFontFactory.CreateFont();
                    }
                    catch (System.IO.IOException e) {
                        throw new SvgProcessingException(SvgLogMessageConstant.FONT_NOT_FOUND, e);
                    }
                }
                currentCanvas.SetFontAndSize(font, fontSize);
                //Current transformation matrix results in the character glyphs being mirrored, correct with inverse tf
                currentCanvas.SetTextMatrix(1, 0, 0, -1, x, y);
                currentCanvas.SetColor(ColorConstants.BLACK, true);
                currentCanvas.ShowText(this.attributesAndStyles.Get(SvgConstants.Attributes.TEXT_CONTENT));
                currentCanvas.EndText();
            }
        }
示例#10
0
        public virtual void TestCompliantThaiPdfA()
        {
            String testName = "testCompliantThaiPdfA";
            String path     = PdfHelper.GetThaiImagePath();
            String pdfPath  = PdfHelper.GetTargetDirectory() + testName + ".pdf";
            OcrPdfCreatorProperties ocrPdfCreatorProperties = new OcrPdfCreatorProperties();

            ocrPdfCreatorProperties.SetPdfLang("en-US");
            ocrPdfCreatorProperties.SetTextColor(DeviceRgb.BLACK);
            FontProvider fontProvider = new FontProvider("Kanit");

            fontProvider.AddFont(PdfHelper.GetKanitFontPath());
            PdfOcrFontProvider pdfOcrFontProvider = new PdfOcrFontProvider(fontProvider.GetFontSet(), "Kanit");

            ocrPdfCreatorProperties.SetFontProvider(pdfOcrFontProvider);
            PdfHelper.CreatePdfA(pdfPath, new FileInfo(path), ocrPdfCreatorProperties, PdfHelper.GetRGBPdfOutputIntent
                                     ());
            String resultWithActualText = PdfHelper.GetTextFromPdfLayerUseActualText(pdfPath, null);

            NUnit.Framework.Assert.AreEqual(PdfHelper.THAI_TEXT, resultWithActualText);
            String resultWithoutUseActualText = PdfHelper.GetTextFromPdfLayer(pdfPath, null);

            NUnit.Framework.Assert.AreEqual(PdfHelper.THAI_TEXT, resultWithoutUseActualText);
            NUnit.Framework.Assert.AreEqual(resultWithoutUseActualText, resultWithActualText);
            ExtractionStrategy strategy = PdfHelper.GetExtractionStrategy(pdfPath);
            PdfFont            font     = strategy.GetPdfFont();
            String             fontName = font.GetFontProgram().GetFontNames().GetFontName();

            NUnit.Framework.Assert.IsTrue(fontName.Contains("Kanit"));
            NUnit.Framework.Assert.IsTrue(font.IsEmbedded());
        }
        internal virtual void ResolveFont(SvgDrawContext context)
        {
            FontProvider provider  = context.GetFontProvider();
            FontSet      tempFonts = context.GetTempFonts();

            font = null;
            if (!provider.GetFontSet().IsEmpty() || (tempFonts != null && !tempFonts.IsEmpty()))
            {
                String fontFamily = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_FAMILY);
                String fontWeight = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_WEIGHT);
                String fontStyle  = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_STYLE);
                fontFamily = fontFamily != null?fontFamily.Trim() : "";

                FontInfo fontInfo = ResolveFontName(fontFamily, fontWeight, fontStyle, provider, tempFonts);
                font = provider.GetPdfFont(fontInfo, tempFonts);
            }
            if (font == null)
            {
                try {
                    // TODO: DEVSIX-2057 each call of createFont() create a new instance of PdfFont.
                    // FontProvider shall be used instead.
                    font = PdfFontFactory.CreateFont();
                }
                catch (System.IO.IOException e) {
                    throw new SvgProcessingException(SvgLogMessageConstant.FONT_NOT_FOUND, e);
                }
            }
        }
示例#12
0
        public virtual void CyrillicAndLatinGroup()
        {
            String       fileName    = "cyrillicAndLatinGroup";
            String       outFileName = destinationFolder + "cyrillicAndLatinGroup.pdf";
            String       cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
            FontProvider sel         = new FontProvider();

            NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "NotoSans-Regular.ttf"));
            NUnit.Framework.Assert.IsTrue(sel.AddFont(fontsFolder + "FreeSans.ttf"));
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H
                                                                   , "Puritan42"));
            String      s      = "Hello world! Здравствуй мир! Hello world! Здравствуй мир!";
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(sel);
            doc.SetProperty(Property.FONT, new String[] { "Puritan42" });
            Text      text      = new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
            Paragraph paragraph = new Paragraph(text);

            doc.Add(paragraph);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff" + fileName));
        }
示例#13
0
        public virtual void WriteTextInFontWhichAliasWithUnicodeChars()
        {
            String fileName    = "writeTextInFontWhichAliasWithUnicodeChars";
            String outFileName = destinationFolder + fileName + ".pdf";
            String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf";
            // フォント3
            String       japaneseAlias = "\u30D5\u30A9\u30F3\u30C83";
            FontProvider provider      = new FontProvider();

            provider.AddFont(fontsFolder + "NotoSans-Regular.ttf");
            provider.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H, japaneseAlias);
            provider.AddFont(fontsFolder + "FreeSans.ttf");
            String      s      = "Hello world!";
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new FileStream(outFileName, FileMode.Create)));
            Document    doc    = new Document(pdfDoc);

            doc.SetFontProvider(provider);
            Paragraph paragraph = new Paragraph(new Text(s).SetBackgroundColor(ColorConstants.LIGHT_GRAY));

            paragraph.SetFontFamily(japaneseAlias);
            doc.Add(paragraph);
            doc.Close();
            // Text shall be written in Puritan 2.0
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             ));
        }
示例#14
0
        public virtual void SearchFontAliasWithUnicodeChars()
        {
            // фонт1
            String cyrillicAlias = "\u0444\u043E\u043D\u04421";
            // γραμματοσειρά2
            String greekAlias = "\u03B3\u03C1\u03B1\u03BC\u03BC\u03B1\u03C4\u03BF\u03C3\u03B5\u03B9\u03C1\u03AC2";
            // フォント3
            String japaneseAlias = "\u30D5\u30A9\u30F3\u30C83";
            IDictionary <String, String> aliasToFontName = new LinkedDictionary <String, String>();

            aliasToFontName.Put(cyrillicAlias, "NotoSans-Regular.ttf");
            aliasToFontName.Put(greekAlias, "FreeSans.ttf");
            aliasToFontName.Put(japaneseAlias, "Puritan2.otf");
            FontProvider provider = new FontProvider();

            foreach (KeyValuePair <String, String> e in aliasToFontName)
            {
                provider.GetFontSet().AddFont(fontsFolder + e.Value, PdfEncodings.IDENTITY_H, e.Key);
            }
            ICollection <String> actualAliases = new HashSet <String>();

            foreach (FontInfo fontInfo in provider.GetFontSet().GetFonts())
            {
                actualAliases.Add(fontInfo.GetAlias());
            }
            ICollection <String> expectedAliases = aliasToFontName.Keys;

            NUnit.Framework.Assert.IsTrue(actualAliases.ContainsAll(expectedAliases) && expectedAliases.ContainsAll(actualAliases
                                                                                                                    ));
            foreach (String fontAlias in expectedAliases)
            {
                PdfFont pdfFont = provider.GetPdfFont(provider.GetFontSelector(JavaCollectionsUtil.SingletonList(fontAlias
                                                                                                                 ), new FontCharacteristics()).BestMatch());
                String fontName = pdfFont.GetFontProgram().GetFontNames().GetFontName();
                NUnit.Framework.Assert.IsTrue(aliasToFontName.Get(fontAlias).Contains(fontName));
            }
        }
示例#15
0
        /// <summary>
        /// Perform OCR using provided path to image (imgPath)
        /// and save result PDF document to "pdfPath".
        /// </summary>
        /// <remarks>
        /// Perform OCR using provided path to image (imgPath)
        /// and save result PDF document to "pdfPath".
        /// (Method is used for compare tool)
        /// </remarks>
        protected internal virtual void DoOcrAndSavePdfToPath(AbstractTesseract4OcrEngine tesseractReader, String
                                                              imgPath, String pdfPath, IList <String> languages, IList <String> fonts, Color color)
        {
            if (languages != null)
            {
                Tesseract4OcrEngineProperties properties = tesseractReader.GetTesseract4OcrEngineProperties();
                properties.SetLanguages(languages);
                tesseractReader.SetTesseract4OcrEngineProperties(properties);
            }
            OcrPdfCreatorProperties properties_1 = new OcrPdfCreatorProperties();

            properties_1.SetPdfLang("en-US");
            properties_1.SetTitle("");
            if (fonts != null && fonts.Count > 0)
            {
                FontProvider fontProvider = new FontProvider();
                foreach (String fontPath in fonts)
                {
                    String name = FONT_PATH_TO_FONT_NAME_MAP.Get(fontPath);
                    fontProvider.GetFontSet().AddFont(fontPath, PdfEncodings.IDENTITY_H, name);
                }
                properties_1.SetFontProvider(fontProvider);
            }
            if (color != null)
            {
                properties_1.SetTextColor(color);
            }
            if (languages != null)
            {
                NUnit.Framework.Assert.AreEqual(languages.Count, tesseractReader.GetTesseract4OcrEngineProperties().GetLanguages
                                                    ().Count);
            }
            OcrPdfCreator ocrPdfCreator = new OcrPdfCreator(tesseractReader, properties_1);

            try {
                using (PdfWriter pdfWriter = GetPdfWriter(pdfPath)) {
                    PdfDocument doc = ocrPdfCreator.CreatePdf(JavaCollectionsUtil.SingletonList <FileInfo>(new FileInfo(imgPath
                                                                                                                        )), pdfWriter);
                    NUnit.Framework.Assert.IsNotNull(doc);
                    doc.Close();
                }
            }
            catch (System.IO.IOException e) {
                LOGGER.Error(e.Message);
            }
        }
示例#16
0
 public virtual void TestInvalidFontWithInvalidDefaultFontFamily()
 {
     NUnit.Framework.Assert.That(() => {
         String testName = "testInvalidFontWithInvalidDefaultFontFamily";
         String path     = PdfHelper.GetDefaultImagePath();
         String pdfPath  = PdfHelper.GetTargetDirectory() + testName + ".pdf";
         FileInfo file   = new FileInfo(path);
         OcrPdfCreatorProperties properties = new OcrPdfCreatorProperties();
         FontProvider pdfOcrFontProvider    = new FontProvider("Font");
         pdfOcrFontProvider.GetFontSet().AddFont("font.ttf", PdfEncodings.IDENTITY_H, "Font");
         properties.SetFontProvider(pdfOcrFontProvider, "Font");
         properties.SetScaleMode(ScaleMode.SCALE_TO_FIT);
         PdfHelper.CreatePdf(pdfPath, file, properties);
         String result = PdfHelper.GetTextFromPdfLayer(pdfPath, null);
         NUnit.Framework.Assert.AreEqual(PdfHelper.DEFAULT_TEXT, result);
         NUnit.Framework.Assert.AreEqual(ScaleMode.SCALE_TO_FIT, properties.GetScaleMode());
     }
                                 , NUnit.Framework.Throws.InstanceOf <OcrException>().With.Message.EqualTo(MessageFormatUtil.Format(OcrException.CANNOT_CREATE_PDF_DOCUMENT, OcrException.CANNOT_RESOLVE_PROVIDED_FONTS)))
     ;
 }
示例#17
0
        public virtual void TestCustomFontInPdf()
        {
            String       testName     = "testDefaultFontInPdf";
            String       path         = PdfHelper.GetDefaultImagePath();
            String       pdfPath      = PdfHelper.GetTargetDirectory() + testName + ".pdf";
            FileInfo     file         = new FileInfo(path);
            FontProvider fontProvider = new FontProvider("FreeSans");

            fontProvider.GetFontSet().AddFont(PdfHelper.GetFreeSansFontPath(), PdfEncodings.IDENTITY_H, "FreeSans");
            OcrPdfCreatorProperties ocrPdfCreatorProperties = new OcrPdfCreatorProperties();

            ocrPdfCreatorProperties.SetPdfLang("en-US");
            ocrPdfCreatorProperties.SetFontProvider(fontProvider, "FreeSans");
            PdfHelper.CreatePdfA(pdfPath, file, ocrPdfCreatorProperties, PdfHelper.GetCMYKPdfOutputIntent());
            ExtractionStrategy strategy = PdfHelper.GetExtractionStrategy(pdfPath);
            PdfFont            font     = strategy.GetPdfFont();
            String             fontName = font.GetFontProgram().GetFontNames().GetFontName();

            NUnit.Framework.Assert.IsTrue(fontName.Contains("FreeSans"));
            NUnit.Framework.Assert.IsTrue(font.IsEmbedded());
        }
示例#18
0
        public virtual void SearchNames2()
        {
            FontProvider sel = new FontProvider();

            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().AddFont(fontsFolder + "NotoSans-Regular.ttf"));
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().AddFont(fontsFolder + "FreeSans.ttf"));
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().AddFont(fontsFolder + "Puritan2.otf", PdfEncodings.IDENTITY_H
                                                                   , "Puritan42"));
            ICollection <FontInfo> fonts = sel.GetFontSet().Get("puritan2");

            NUnit.Framework.Assert.IsTrue(fonts.Count != 0, "Puritan not found!");
            FontInfo puritan = GetFirst(fonts);

            fonts = sel.GetFontSet().Get("NotoSans");
            NUnit.Framework.Assert.IsTrue(fonts.Count != 0, "NotoSans not found!");
            FontInfo notoSans = GetFirst(fonts);

            fonts = sel.GetFontSet().Get("FreeSans");
            NUnit.Framework.Assert.IsTrue(fonts.Count != 0, "FreeSans not found!");
            FontInfo freeSans = GetFirst(fonts);

            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Contains("NotoSans"), "NotoSans not found!");
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Contains("Noto Sans"), "NotoSans not found!");
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Contains("FreeSans"), "FreeSans not found!");
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Contains("Free Sans"), "FreeSans not found!");
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Contains("puritan 2.0 regular"), "Puritan 2.0 not found!");
            NUnit.Framework.Assert.IsTrue(sel.GetFontSet().Contains("puritan2"), "Puritan 2.0 not found!");
            NUnit.Framework.Assert.IsFalse(sel.GetFontSet().Contains("puritan42"), "Puritan42 found!");
            NUnit.Framework.Assert.AreEqual(notoSans, GetFirst(sel.GetFontSet().Get("NotoSans")), "NotoSans not found!"
                                            );
            NUnit.Framework.Assert.AreEqual(notoSans, GetFirst(sel.GetFontSet().Get("Noto Sans")), "NotoSans not found!"
                                            );
            NUnit.Framework.Assert.AreEqual(freeSans, GetFirst(sel.GetFontSet().Get("FreeSans")), "FreeSans not found!"
                                            );
            NUnit.Framework.Assert.AreEqual(freeSans, GetFirst(sel.GetFontSet().Get("Free Sans")), "FreeSans not found!"
                                            );
            NUnit.Framework.Assert.AreEqual(puritan, GetFirst(sel.GetFontSet().Get("puritan 2.0 regular")), "Puritan 2.0 not found!"
                                            );
            NUnit.Framework.Assert.AreEqual(puritan, GetFirst(sel.GetFontSet().Get("puritan2")), "Puritan 2.0 not found!"
                                            );
            NUnit.Framework.Assert.IsTrue(GetFirst(sel.GetFontSet().Get("puritan42")) == null, "Puritan42 found!");
        }
 /// <summary>Check fonts in font provider and temporary font set.</summary>
 /// <returns>true, if there is at least one font either in FontProvider or temporary FontSet.</returns>
 /// <seealso cref="AddTemporaryFont(iText.Layout.Font.FontInfo, System.String)"/>
 /// <seealso cref="AddTemporaryFont(iText.IO.Font.FontProgram, System.String, System.String)"/>
 public virtual bool HasFonts()
 {
     return(!fontProvider.GetFontSet().IsEmpty() || (tempFonts != null && !tempFonts.IsEmpty()));
 }