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()); }
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()); }
/// <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); } }
public virtual void TestInvalidCustomFontInPdfACMYK() { String testName = "testInvalidCustomFontInPdf"; String path = PdfHelper.GetDefaultImagePath(); String pdfPath = PdfHelper.GetTargetDirectory() + testName + ".pdf"; FileInfo file = new FileInfo(path); OcrPdfCreatorProperties ocrPdfCreatorProperties = new OcrPdfCreatorProperties(); ocrPdfCreatorProperties.SetPdfLang("en-US"); ocrPdfCreatorProperties.SetFontProvider(new PdfOcrFontProvider()); 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("LiberationSans")); NUnit.Framework.Assert.IsTrue(font.IsEmbedded()); }
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))) ; }
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()); }