public virtual void CreateDocumentWithType3Font() { String testName = "DocumentWithType3Font"; String filename = destinationFolder + testName + ".pdf"; String cmpFilename = sourceFolder + "cmp_" + testName + ".pdf"; PdfDocument pdfDoc = CreateDocument(filename); PdfType3Font type3Font = PdfFontFactory.CreateType3Font(pdfDoc, false); Type3Glyph type3Glyph = type3Font.AddGlyph('A', 600, 0, 0, 600, 700); type3Glyph.SetLineWidth(100); type3Glyph.MoveTo(5, 5); type3Glyph.LineTo(300, 695); type3Glyph.LineTo(595, 5); type3Glyph.ClosePathFillStroke(); PdfPage page = pdfDoc.AddNewPage(); PdfCanvas canvas = new PdfCanvas(page); canvas.SaveState().BeginText().SetFontAndSize(type3Font, 36).MoveText(50, 700).ShowText("AA").EndText(); type3Font = PdfFontFactory.CreateType3Font(pdfDoc, false); type3Glyph = type3Font.AddGlyph('A', 600, 0, 0, 600, 700); type3Glyph.SetLineWidth(100); type3Glyph.MoveTo(5, 5); type3Glyph.LineTo(300, 695); type3Glyph.LineTo(595, 5); type3Glyph.ClosePathFillStroke(); canvas = new PdfCanvas(page); canvas.SaveState().BeginText().SetFontAndSize(type3Font, 36).MoveText(50, 650).ShowText("AAA").EndText(); pdfDoc.Close(); //PdfType3Font comparing returns false; NUnit.Framework.Assert.AreEqual(2, CountPdfFonts(filename)); // reading and comparing text NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpFilename, destinationFolder, "diff_")); }
public virtual void StandardAndType3Fonts() { String fileName = "taggedDocumentWithType3Font"; String srcFileName = sourceFolder + "src_" + fileName + ".pdf"; String outFileName = destinationFolder + fileName + ".pdf"; String cmpFileName = sourceFolder + "cmp_" + fileName + ".pdf"; FontProviderTest.PdfFontProvider sel = new FontProviderTest.PdfFontProvider(); sel.AddStandardPdfFonts(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(new FileStream(srcFileName, FileMode.Open, FileAccess.Read )), new PdfWriter(new FileStream(outFileName, FileMode.Create))); PdfType3Font pdfType3Font = (PdfType3Font)PdfFontFactory.CreateFont((PdfDictionary)pdfDoc.GetPdfObject(5)); sel.AddPdfFont(pdfType3Font, "CustomFont"); Document doc = new Document(pdfDoc); doc.SetFontProvider(sel); Paragraph paragraph = new Paragraph("Next paragraph contains a triangle, actually Type 3 Font"); paragraph.SetProperty(Property.FONT, new String[] { StandardFontFamilies.TIMES }); doc.Add(paragraph); paragraph = new Paragraph("A"); paragraph.SetFontFamily("CustomFont"); doc.Add(paragraph); paragraph = new Paragraph("Next paragraph"); paragraph.SetProperty(Property.FONT, new String[] { StandardFonts.COURIER }); doc.Add(paragraph); doc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder , "diff" + fileName)); }
public virtual void GetFontWithDirectFontDictionaryTest() { PdfDictionary initialFontDict = new PdfDictionary(); initialFontDict.Put(PdfName.Subtype, PdfName.Type3); initialFontDict.Put(PdfName.FontMatrix, new PdfArray(new float[] { 0.001F, 0, 0, 0.001F, 0, 0 })); initialFontDict.Put(PdfName.Widths, new PdfArray()); PdfDictionary encoding = new PdfDictionary(); initialFontDict.Put(PdfName.Encoding, encoding); PdfArray differences = new PdfArray(); differences.Add(new PdfNumber(AdobeGlyphList.NameToUnicode("a"))); differences.Add(new PdfName("a")); encoding.Put(PdfName.Differences, differences); NUnit.Framework.Assert.IsNull(initialFontDict.GetIndirectReference()); using (PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) { // prevent no pages exception on close doc.AddNewPage(); PdfType3Font font1 = (PdfType3Font)doc.GetFont(initialFontDict); NUnit.Framework.Assert.IsNotNull(font1); // prevent no glyphs for type3 font on close font1.AddGlyph('a', 0, 0, 0, 0, 0); } }
public virtual void TestType3FontWithDifferences() { String sourcePdf = sourceFolder + "DocumentWithType3FontWithDifferences.pdf"; String comparedTextFile = sourceFolder + "textFromDocWithType3FontWithDifferences.txt"; PdfDocument pdf = new PdfDocument(new PdfReader(sourcePdf)); String result = PdfTextExtractor.GetTextFromPage(pdf.GetPage(1), new LocationTextExtractionStrategy()); PdfDictionary pdfType3FontDict = (PdfDictionary)pdf.GetPdfObject(292); PdfType3Font pdfType3Font = (PdfType3Font)PdfFontFactory.CreateFont(pdfType3FontDict); pdf.Close(); byte[] bytes = File.ReadAllBytes(System.IO.Path.Combine(comparedTextFile)); NUnit.Framework.Assert.AreEqual(iText.IO.Util.JavaUtil.GetStringForBytes(bytes, System.Text.Encoding.UTF8) , result); NUnit.Framework.Assert.AreEqual(83, pdfType3Font.GetNumberOfGlyphs()); NUnit.Framework.Assert.AreEqual("gA", pdfType3Font.GetFontEncoding().GetDifference(10)); NUnit.Framework.Assert.AreEqual(41, pdfType3Font.GetFontProgram().GetGlyphByCode(10).GetUnicode()); NUnit.Framework.Assert.AreEqual(".notdef", pdfType3Font.GetFontEncoding().GetDifference(210)); NUnit.Framework.Assert.AreEqual(928, pdfType3Font.GetFontProgram().GetGlyphByCode(210).GetUnicode()); }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run() { PdfFixedDocument document = new PdfFixedDocument(); PdfPage page = document.Pages.Add(); PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 20); PdfBrush blackBrush = new PdfBrush(PdfRgbColor.Black); page.Graphics.DrawString("The digits below, from 0 to 9, are drawn using a Type3 font.", helvetica, blackBrush, 50, 100); PdfType3Font t3 = new PdfType3Font("DemoT3"); t3.Size = 24; t3.FirstChar = (byte)' '; t3.LastChar = (byte)'9'; t3.FontMatrix = new PdfMatrix(0.01, 0, 0, 0.01, 0, 0); double[] widths = new double[] { 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; t3.Widths = widths; PdfPen hollowPen = new PdfPen(null, 8); PdfBrush hollowBrush = new PdfBrush(null); // space PdfType3Glyph t3s = new PdfType3Glyph(0x20, new PdfSize(100, 100)); t3.Glyphs.Add(t3s); // 0 PdfType3Glyph t30 = new PdfType3Glyph(0x30, new PdfSize(100, 100)); t30.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t30.Graphics.CompressAndClose(); t3.Glyphs.Add(t30); // 1 PdfType3Glyph t31 = new PdfType3Glyph(0x31, new PdfSize(100, 100)); t31.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t31.Graphics.DrawEllipse(hollowBrush, 40, 40, 20, 20); t31.Graphics.CompressAndClose(); t3.Glyphs.Add(t31); // 2 PdfType3Glyph t32 = new PdfType3Glyph(0x32, new PdfSize(100, 100)); t32.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t32.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t32.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t32.Graphics.CompressAndClose(); t3.Glyphs.Add(t32); // 3 PdfType3Glyph t33 = new PdfType3Glyph(0x33, new PdfSize(100, 100)); t33.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t33.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t33.Graphics.DrawEllipse(hollowBrush, 40, 40, 20, 20); t33.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t33.Graphics.CompressAndClose(); t3.Glyphs.Add(t33); // 4 PdfType3Glyph t34 = new PdfType3Glyph(0x34, new PdfSize(100, 100)); t34.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t34.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t34.Graphics.DrawEllipse(hollowBrush, 65, 15, 20, 20); t34.Graphics.DrawEllipse(hollowBrush, 15, 65, 20, 20); t34.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t34.Graphics.CompressAndClose(); t3.Glyphs.Add(t34); // 5 PdfType3Glyph t35 = new PdfType3Glyph(0x35, new PdfSize(100, 100)); t35.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t35.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t35.Graphics.DrawEllipse(hollowBrush, 65, 15, 20, 20); t35.Graphics.DrawEllipse(hollowBrush, 40, 40, 20, 20); t35.Graphics.DrawEllipse(hollowBrush, 15, 65, 20, 20); t35.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t35.Graphics.CompressAndClose(); t3.Glyphs.Add(t35); // 6 PdfType3Glyph t36 = new PdfType3Glyph(0x36, new PdfSize(100, 100)); t36.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t36.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t36.Graphics.DrawEllipse(hollowBrush, 65, 15, 20, 20); t36.Graphics.DrawEllipse(hollowBrush, 15, 40, 20, 20); t36.Graphics.DrawEllipse(hollowBrush, 65, 40, 20, 20); t36.Graphics.DrawEllipse(hollowBrush, 15, 65, 20, 20); t36.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t36.Graphics.CompressAndClose(); t3.Glyphs.Add(t36); // 7 PdfType3Glyph t37 = new PdfType3Glyph(0x37, new PdfSize(100, 100)); t37.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t37.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t37.Graphics.DrawEllipse(hollowBrush, 65, 15, 20, 20); t37.Graphics.DrawEllipse(hollowBrush, 15, 40, 20, 20); t37.Graphics.DrawEllipse(hollowBrush, 40, 40, 20, 20); t37.Graphics.DrawEllipse(hollowBrush, 65, 40, 20, 20); t37.Graphics.DrawEllipse(hollowBrush, 15, 65, 20, 20); t37.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t37.Graphics.CompressAndClose(); t3.Glyphs.Add(t37); // 8 PdfType3Glyph t38 = new PdfType3Glyph(0x38, new PdfSize(100, 100)); t38.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t38.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t38.Graphics.DrawEllipse(hollowBrush, 40, 15, 20, 20); t38.Graphics.DrawEllipse(hollowBrush, 65, 15, 20, 20); t38.Graphics.DrawEllipse(hollowBrush, 15, 40, 20, 20); t38.Graphics.DrawEllipse(hollowBrush, 65, 40, 20, 20); t38.Graphics.DrawEllipse(hollowBrush, 15, 65, 20, 20); t38.Graphics.DrawEllipse(hollowBrush, 40, 65, 20, 20); t38.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t38.Graphics.CompressAndClose(); t3.Glyphs.Add(t38); // 9 PdfType3Glyph t39 = new PdfType3Glyph(0x39, new PdfSize(100, 100)); t39.Graphics.DrawRectangle(hollowPen, 5, 5, 90, 90); t39.Graphics.DrawEllipse(hollowBrush, 15, 15, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 40, 15, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 65, 15, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 15, 40, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 40, 40, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 65, 40, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 15, 65, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 40, 65, 20, 20); t39.Graphics.DrawEllipse(hollowBrush, 65, 65, 20, 20); t39.Graphics.CompressAndClose(); t3.Glyphs.Add(t39); PdfBrush paleVioletRedbrush = new PdfBrush(PdfRgbColor.PaleVioletRed); page.Graphics.DrawString("0 1 2 3 4 5 6 7 8 9", t3, paleVioletRedbrush, 50, 150); PdfBrush midnightBluebrush = new PdfBrush(PdfRgbColor.MidnightBlue); page.Graphics.DrawString("0 1 2 3 4 5 6 7 8 9", t3, midnightBluebrush, 50, 200); SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.type3fonts.pdf") }; return output; }
protected void ManipulatePdf(String dest) { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc); /* Create a custom type 3 font. * The second argument defines whether the glyph color is specified * in the glyph description in the font. */ PdfType3Font t3 = PdfFontFactory.CreateType3Font(pdfDoc, true); float linewidth = 125; /* Define I letter of the custom font. * If the character was already defined, addGlyph() method will return the same content. * If the colorized parameter of PdfType3Font instance is set to true, * then parameters of the glyph bounding box will be ignored. */ PdfCanvas i = t3.AddGlyph('I', 700, 0, 0, 1200, 600); i.SetLineWidth(10); i.SetStrokeColor(new DeviceRgb(0xf9, 0x9d, 0x25)); i.SetLineWidth(linewidth); i.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND); i.MoveTo(600, 36); i.LineTo(600, 564); i.Stroke(); // Define T letter of the custom font PdfCanvas t = t3.AddGlyph('T', 1170, 0, 0, 1200, 600); t.SetLineWidth(10); t.SetStrokeColor(new DeviceRgb(0x08, 0x49, 0x75)); t.SetLineWidth(linewidth); t.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND); t.MoveTo(144, 564); t.LineTo(1056, 564); t.MoveTo(600, 36); t.LineTo(600, 564); t.Stroke(); // Define E letter of the custom font PdfCanvas e = t3.AddGlyph('E', 1150, 0, 0, 1200, 600); e.SetLineWidth(10); e.SetStrokeColor(new DeviceRgb(0xf8, 0x9b, 0x22)); e.SetLineWidth(linewidth); e.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND); e.MoveTo(144, 36); e.LineTo(1056, 36); e.MoveTo(144, 300); e.LineTo(1056, 300); e.MoveTo(144, 564); e.LineTo(1056, 564); e.Stroke(); // Define X letter of the custom font PdfCanvas x = t3.AddGlyph('X', 1160, 0, 0, 1200, 600); x.SetStrokeColor(new DeviceRgb(0x10, 0x46, 0x75)); x.SetLineWidth(10); x.SetLineWidth(linewidth); x.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND); x.MoveTo(144, 36); x.LineTo(1056, 564); x.MoveTo(144, 564); x.LineTo(1056, 36); x.Stroke(); Paragraph p = new Paragraph("ITEXT") .SetFont(t3) .SetFontSize(20); doc.Add(p); p = new Paragraph("I\nT\nE\nX\nT") .SetFixedLeading(20) .SetFont(t3) .SetFontSize(20); doc.Add(p); doc.Close(); }