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()); }
public PdfFontDetails CreateFont(PdfFont pdfFont) { var font = new PdfFontDetails(); var fontName = pdfFont.GetFontProgram().GetFontNames().GetFontName(); font.FontFamily = fontName; var basicFontFamily = fontFamilyRegex.Replace(fontName, ""); basicFontFamily = new string(SpaceInCamelCase(basicFontFamily).ToArray()); font.BasicFontFamily = basicFontFamily; font.Bold = pdfFont.GetFontProgram().GetFontNames().GetFontWeight() >= FontWeights.BOLD || fontName.Contains("bold") || fontName.Contains("Bold"); font.Italic = fontName.Contains("italic") || fontName.Contains("Italic"); return(font); }
public override void CheckFont(PdfFont pdfFont) { if (!pdfFont.IsEmbedded()) { throw new PdfAConformanceException(PdfAConformanceException.ALL_THE_FONTS_MUST_BE_EMBEDDED_THIS_ONE_IS_NOT_0 ).SetMessageParams(pdfFont.GetFontProgram().GetFontNames().GetFontName()); } if (pdfFont is PdfTrueTypeFont) { PdfTrueTypeFont trueTypeFont = (PdfTrueTypeFont)pdfFont; bool symbolic = trueTypeFont.GetFontEncoding().IsFontSpecific(); if (symbolic) { CheckSymbolicTrueTypeFont(trueTypeFont); } else { CheckNonSymbolicTrueTypeFont(trueTypeFont); } } if (pdfFont is PdfType3Font) { PdfDictionary charProcs = pdfFont.GetPdfObject().GetAsDictionary(PdfName.CharProcs); foreach (PdfName charName in charProcs.KeySet()) { CheckContentStream(charProcs.GetAsStream(charName)); } } }
private void UpdateColor(IList <LineRenderer> lines) { foreach (LineRenderer renderer in lines) { IList <IRenderer> children = renderer.GetChildRenderers(); foreach (IRenderer child in children) { if (child is TextRenderer) { PdfFont pdfFont = ((TextRenderer)child).GetPropertyAsFont(Property.FONT); if (null != pdfFont) { Color updatedColor = fontColorMap[pdfFont.GetFontProgram().GetFontNames().GetFontName()]; if (null != updatedColor) { // Although setting a property via setProperty might be useful, // it's regarded as internal iText functionality. The properties are expected // to be set on elements via specific setters (setFont, for example). // iText doesn't guarantee that setProperty will work as you expect it, // so please be careful while calling this method. child.SetProperty(Property.FONT_COLOR, new TransparentColor(updatedColor)); } } } } } }
/// <summary>Replaces special whitespace glyphs to new whitespace '\u0020' glyph that has custom width.</summary> /// <remarks> /// Replaces special whitespace glyphs to new whitespace '\u0020' glyph that has custom width. /// Special whitespace glyphs are symbols such as '\u2002', '\u2003', '\u2009' and '\t'. /// </remarks> /// <param name="line">the string for preprocessing</param> /// <param name="font">the font that will be used when displaying the string</param> /// <returns>old line with new special whitespace glyphs</returns> public static GlyphLine ReplaceSpecialWhitespaceGlyphs(GlyphLine line, PdfFont font) { if (null != line) { bool isMonospaceFont = font.GetFontProgram().GetFontMetrics().IsFixedPitch(); Glyph space = font.GetGlyph('\u0020'); int spaceWidth = space.GetWidth(); Glyph glyph; int lineSize = line.Size(); for (int i = 0; i < lineSize; i++) { glyph = line.Get(i); int xAdvance = 0; bool isSpecialWhitespaceGlyph = false; if (glyph.GetCode() <= 0) { switch (glyph.GetUnicode()) { // ensp case '\u2002': { xAdvance = isMonospaceFont ? 0 : 500 - spaceWidth; isSpecialWhitespaceGlyph = true; break; } // emsp case '\u2003': { xAdvance = isMonospaceFont ? 0 : 1000 - spaceWidth; isSpecialWhitespaceGlyph = true; break; } // thinsp case '\u2009': { xAdvance = isMonospaceFont ? 0 : 200 - spaceWidth; isSpecialWhitespaceGlyph = true; break; } case '\t': { xAdvance = 3 * spaceWidth; isSpecialWhitespaceGlyph = true; break; } } } if (isSpecialWhitespaceGlyph) { Glyph newGlyph = new Glyph(space, glyph.GetUnicode()); System.Diagnostics.Debug.Assert(xAdvance <= short.MaxValue && xAdvance >= short.MinValue); newGlyph.SetXAdvance((short)xAdvance); line.Set(i, newGlyph); } } } return(line); }
public virtual void AddPdfFont(PdfFont font, String alias) { FontInfo fontInfo = FontInfo.Create(font.GetFontProgram(), null, alias); // stored FontInfo will be used in FontSelector collection. pdfFontInfos.Add(fontInfo); // first of all FOntProvider search PdfFont in pdfFonts. pdfFonts.Put(fontInfo, font); }
public virtual void TestThaiImageWithNotDefGlyphs() { String testName = "testThaiImageWithNotdefGlyphs"; String path = PdfHelper.GetThaiImagePath(); String pdfPath = PdfHelper.GetTargetDirectory() + testName + ".pdf"; PdfHelper.CreatePdf(pdfPath, new FileInfo(path), new OcrPdfCreatorProperties().SetTextColor(DeviceRgb.BLACK )); ExtractionStrategy strategy = PdfHelper.GetExtractionStrategy(pdfPath); PdfFont font = strategy.GetPdfFont(); String fontName = font.GetFontProgram().GetFontNames().GetFontName(); NUnit.Framework.Assert.IsTrue(fontName.Contains("LiberationSans")); }
public override bool Accept(IEventData data, EventType type) { if (type.Equals(EventType.RENDER_TEXT)) { TextRenderInfo renderInfo = (TextRenderInfo)data; PdfFont font = renderInfo.GetFont(); if (null != font) { String fontName = font.GetFontProgram().GetFontNames().GetFontName(); return(fontName.EndsWith("Bold") || fontName.EndsWith("Oblique")); } } return(false); }
public virtual void TestDefaultFontInPdfARgb() { String testName = "testDefaultFontInPdf"; String path = PdfHelper.GetDefaultImagePath(); String pdfPath = PdfHelper.GetTargetDirectory() + testName + ".pdf"; FileInfo file = new FileInfo(path); OcrPdfCreatorProperties ocrPdfCreatorProperties = new OcrPdfCreatorProperties(); ocrPdfCreatorProperties.SetPdfLang("en-US"); ocrPdfCreatorProperties.SetTextColor(DeviceRgb.BLACK); PdfHelper.CreatePdfA(pdfPath, file, ocrPdfCreatorProperties, PdfHelper.GetRGBPdfOutputIntent()); 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 ResolveFirstPdfFontWithGlyphsAvailableOnlyInSecondaryFont() { // Test that in TextRenderer the #resolveFirstPdfFont method is overloaded in such way // that yielded font contains at least some of the glyphs for the text characters. Text text = new Text("\u043A\u0456\u0440\u044B\u043B\u0456\u0446\u0430"); // "кірыліца" // Puritan doesn't contain cyrillic symbols, while Noto Sans does. text.SetFontFamily(JavaUtil.ArraysAsList("Puritan 2.0", "Noto Sans")); FontProvider fontProvider = new FontProvider(); fontProvider.AddFont(FONTS_FOLDER + "Puritan2.otf"); fontProvider.AddFont(FONTS_FOLDER + "NotoSans-Regular.ttf"); text.SetProperty(Property.FONT_PROVIDER, fontProvider); TextRenderer renderer = (TextRenderer) new TextRenderer(text); PdfFont pdfFont = renderer.ResolveFirstPdfFont(); NUnit.Framework.Assert.AreEqual("NotoSans", pdfFont.GetFontProgram().GetFontNames().GetFontName()); }
public override void CheckFont(PdfFont pdfFont) { if (!pdfFont.IsEmbedded()) { throw new PdfAConformanceException(PdfAConformanceException.AllFontsMustBeEmbeddedThisOneIsnt1).SetMessageParams (pdfFont.GetFontProgram().GetFontNames().GetFontName()); } if (pdfFont is PdfTrueTypeFont) { PdfTrueTypeFont trueTypeFont = (PdfTrueTypeFont)pdfFont; bool symbolic = trueTypeFont.GetFontEncoding().IsFontSpecific(); if (symbolic) { CheckSymbolicTrueTypeFont(trueTypeFont); } else { CheckNonSymbolicTrueTypeFont(trueTypeFont); } } }
public override void CheckFont(PdfFont pdfFont) { if (!pdfFont.IsEmbedded()) { throw new PdfAConformanceException(PdfAConformanceException.ALL_THE_FONTS_MUST_BE_EMBEDDED_THIS_ONE_IS_NOT_0 ).SetMessageParams(pdfFont.GetFontProgram().GetFontNames().GetFontName()); } if (pdfFont is PdfTrueTypeFont) { PdfTrueTypeFont trueTypeFont = (PdfTrueTypeFont)pdfFont; bool symbolic = trueTypeFont.GetFontEncoding().IsFontSpecific(); if (symbolic) { CheckSymbolicTrueTypeFont(trueTypeFont); } else { CheckNonSymbolicTrueTypeFont(trueTypeFont); } } }
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()); }
protected void EmbedFont(String merged, String fontfile, String result) { // The font file FileStream raf = new FileStream(fontfile, FileMode.Open, FileAccess.Read); byte[] fontbytes = new byte[(int)raf.Length]; raf.Read(fontbytes, 0, fontbytes.Length); raf.Close(); // Create a new stream for the font file PdfStream stream = new PdfStream(fontbytes); stream.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION); stream.Put(PdfName.Length1, new PdfNumber(fontbytes.Length)); PdfDocument pdfDoc = new PdfDocument(new PdfReader(merged), new PdfWriter(result)); int numberOfPdfObjects = pdfDoc.GetNumberOfPdfObjects(); // Search for the font dictionary for (int i = 0; i < numberOfPdfObjects; i++) { PdfObject pdfObject = pdfDoc.GetPdfObject(i); if (pdfObject == null || !pdfObject.IsDictionary()) { continue; } PdfDictionary fontDictionary = (PdfDictionary)pdfObject; PdfFont font = PdfFontFactory.CreateFont(fontfile, PdfEncodings.WINANSI); PdfName fontname = new PdfName(font.GetFontProgram().GetFontNames().GetFontName()); if (PdfName.FontDescriptor.Equals(fontDictionary.Get(PdfName.Type)) && fontname.Equals(fontDictionary.Get(PdfName.FontName))) { // Embed the passed font to the pdf document fontDictionary.Put(PdfName.FontFile2, stream.MakeIndirect(pdfDoc).GetIndirectReference()); } } pdfDoc.Close(); }
private int GetInfo(String character, PdfAcroForm form, String name) { PdfFormField field = form.GetField(name); PdfFont font = field.GetFont(); FontMetrics fontMetrics = font.GetFontProgram().GetFontMetrics(); float fontSize = field.GetFontSize(); if (fontSize == 0) { return(1000); } Rectangle rectangle = field.GetWidgets()[0].GetRectangle().ToRectangle(); // Factor here is a leading value. We calculate it by subtracting lower left corner value from // the upper right corner value of the glyph bounding box float factor = (fontMetrics.GetBbox()[3] - fontMetrics.GetBbox()[1]) / 1000f; int rows = (int)Math.Round(rectangle.GetHeight() / (fontSize * factor) + 0.5f); int columns = (int)Math.Round(rectangle.GetWidth() / font.GetWidth(character, fontSize) + 0.5f); return(rows * columns); }
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)); } }
protected internal virtual float GetDescender() { float sizeCoef = size / FontProgram.UNITS_NORMALIZATION; return(font.GetFontProgram().GetFontMetrics().GetTypoDescender() * sizeCoef); }