//This function will find every image in the document and add text if //possible static void AddTextToImages(Document doc, Content content, OCREngine engine) { for (int index = 0; index < content.NumElements; index++) { Element e = content.GetElement(index); if (e is Datalogics.PDFL.Image) { //PlaceTextUnder creates a form with the image and the generated text //under the image. The original image in the page is then replaced by //by the form. Form form = engine.PlaceTextUnder((Image)e, doc); content.RemoveElement(index); content.AddElement(form, index - 1); } else if (e is Container) { AddTextToImages(doc, (e as Container).Content, engine); } else if (e is Group) { AddTextToImages(doc, (e as Group).Content, engine); } else if (e is Form) { AddTextToImages(doc, (e as Form).Content, engine); } } }
static void Main(string[] args) { using (Library lib = new Library()) { String sOutput = "../Indexed-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); ColorSpace baseCS = ColorSpace.DeviceRGB; List <Int32> lookup = new List <Int32>(); int[] lowhi = { 0, 255 }; foreach (int r in lowhi) { foreach (int g in lowhi) { foreach (int b in lowhi) { lookup.Add(r); lookup.Add(g); lookup.Add(b); } } } IndexedColorSpace cs = new IndexedColorSpace(baseCS, 7, lookup); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 4.0 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { using (Library lib = new Library()) { String sOutput = "../CalRGB-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); // a CalRGB color space for the CCIR XA/11-recommended D65 // white point with 1.8 gammas and Sony Trinitron phosphor // chromaticities // // Plus a dummy value for testing the black point Double[] whitePoint = { 0.9505, 1.0000, 1.0890 }; Double[] blackPoint = { 0.0, 0.0, 0.0 }; Double[] gamma = { 1.8, 1.8, 1.8 }; Double[] matrix = { 0.4497, 0.2446, 0.0252, 0.3163, 0.6720, 0.1412, 0.1845, 0.0833, 0.9227 }; ColorSpace cs = new CalRGBColorSpace(whitePoint, blackPoint, gamma, matrix); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 0.3, 0.7, 0.3 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void ResampleImages(Content content) { int i = 0; while (i < content.NumElements) { Element e = content.GetElement(i); Console.WriteLine(i + " / " + content.NumElements + " = " + e.GetType()); if (e is Image) { Image img = (Image)e; try { Image newimg = img.ChangeResolution(400); Console.WriteLine("Replacing an image..."); content.AddElement(newimg, i); content.RemoveElement(i); Console.WriteLine("Replaced."); numreplaced++; } catch (ApplicationException ex) { Console.WriteLine(ex.Message); } } else if (e is Container) { Console.WriteLine("Recursing through a Container"); ResampleImages((e as Container).Content); } else if (e is Group) { Console.WriteLine("Recursing through a Group"); ResampleImages((e as Group).Content); } else if (e is Form) { Console.WriteLine("Recursing through a Form"); Content formcontent = (e as Form).Content; ResampleImages(formcontent); (e as Form).Content = formcontent; } i++; } }
static void Main(string[] args) { using (Library lib = new Library()) { String sOutput = "../Separation-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); ColorSpace alternate = ColorSpace.DeviceRGB; Double[] domain = { 0.0, 1.0 }; int nOutputs = 3; Double[] range = { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 }; Double[] C0 = { 0.0, 0.0, 0.0 }; Double[] C1 = { 1.0, 0.0, 0.0 }; Function tintTransform = new ExponentialFunction(domain, nOutputs, C0, C1, 1.0); tintTransform.Range = range; ColorSpace cs = new SeparationColorSpace("DLColor", alternate, tintTransform); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 1.0 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { using (Library lib = new Library()) { String sOutput = "../Lab-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); // CIE 1976 L*a*b* space with the CCIR XA/11-recommended D65 // white point. The a* and b* components, although // theoretically unbounded, are defined to lie in the useful // range -128 to +127 Double[] whitePoint = { 0.9505, 1.0000, 1.0890 }; Double[] blackPoint = { 0.0, 0.0, 0.0 }; Double[] range = { -128.0, 127.0, -128.0, 127.0 }; ColorSpace cs = new LabColorSpace(whitePoint, blackPoint, range); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 55, -54, 55 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { using (Library lib = new Library()) { String sInput = Library.ResourceDirectory + "Sample_Input/sRGB_IEC61966-2-1_noBPC.icc"; String sOutput = "../ICCBased-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); FileStream stream = new FileStream(sInput, FileMode.Open); PDFStream pdfStream = new PDFStream(stream, doc, null, null); ColorSpace cs = new ICCBasedColorSpace(pdfStream, 3); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 1.0, 0.0, 0.0 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { using (Library lib = new Library()) { String sOutput = "../CalGray-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); // a space consisting of the Y dimension of the CIE 1931 XYZ // space with the CCIR XA/11-recommended D65 white point and // opto-electronic transfer function. Double[] whitePoint = { 0.9505, 1.0000, 1.0890 }; Double[] blackPoint = { 0.0, 0.0, 0.0 }; double gamma = 2.2222; ColorSpace cs = new CalGrayColorSpace(whitePoint, blackPoint, gamma); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 0.5 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { using (Library lib = new Library()) { String sOutput = "../DeviceN-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); ColorSpace alternate = ColorSpace.DeviceRGB; Double[] domain = { 0.0, 1.0, 0.0, 1.0 }; Double[] range = { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 }; string code = "{ 0 exch }"; Function tintTransform = new PostScriptCalculatorFunction(domain, range, code); ColorSpace cs = new DeviceNColorSpace(new string[] { "DLRed", "DLBlue" }, alternate, tintTransform); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 0.75, 0.75 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { // ReSharper disable once UnusedVariable using (Library lib = new Library()) { String sOutput = "Lab-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font; try { font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); } catch (ApplicationException ex) { if (ex.Message.Equals("The specified font could not be found.") && System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices .OSPlatform.Linux) && !System.IO.Directory.Exists("/usr/share/fonts/msttcore/")) { Console.WriteLine("Please install Microsoft Core Fonts on Linux first."); return; } throw; } // CIE 1976 L*a*b* space with the CCIR XA/11-recommended D65 // white point. The a* and b* components, although // theoretically unbounded, are defined to lie in the useful // range -128 to +127 Double[] whitePoint = { 0.9505, 1.0000, 1.0890 }; Double[] blackPoint = { 0.0, 0.0, 0.0 }; Double[] range = { -128.0, 127.0, -128.0, 127.0 }; ColorSpace cs = new LabColorSpace(whitePoint, blackPoint, range); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new Double[] { 55, -54, 55 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { // ReSharper disable once UnusedVariable using (Library lib = new Library()) { String sOutput = "CalRGB-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font; try { font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); } catch (ApplicationException ex) { if (ex.Message.Equals("The specified font could not be found.") && System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices .OSPlatform.Linux) && !System.IO.Directory.Exists("/usr/share/fonts/msttcore/")) { Console.WriteLine("Please install Microsoft Core Fonts on Linux first."); return; } throw; } // a CalRGB color space for the CCIR XA/11-recommended D65 // white point with 1.8 gammas and Sony Trinitron phosphor // chromaticities // // Plus a dummy value for testing the black point Double[] whitePoint = { 0.9505, 1.0000, 1.0890 }; Double[] blackPoint = { 0.0, 0.0, 0.0 }; Double[] gamma = { 1.8, 1.8, 1.8 }; Double[] matrix = { 0.4497, 0.2446, 0.0252, 0.3163, 0.6720, 0.1412, 0.1845, 0.0833, 0.9227 }; ColorSpace cs = new CalRGBColorSpace(whitePoint, blackPoint, gamma, matrix); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new[] { 0.3, 0.7, 0.3 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { // ReSharper disable once UnusedVariable using (Library lib = new Library()) { String sInput = Library.ResourceDirectory + "Sample_Input/sRGB_IEC61966-2-1_noBPC.icc"; String sOutput = "ICCBased-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font; try { font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); } catch (ApplicationException ex) { if (ex.Message.Equals("The specified font could not be found.") && System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices .OSPlatform.Linux) && !Directory.Exists("/usr/share/fonts/msttcore/")) { Console.WriteLine("Please install Microsoft Core Fonts on Linux first."); return; } throw; } FileStream stream = new FileStream(sInput, FileMode.Open); PDFStream pdfStream = new PDFStream(stream, doc, null, null); ColorSpace cs = new ICCBasedColorSpace(pdfStream, 3); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new[] { 1.0, 0.0, 0.0 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { // ReSharper disable once UnusedVariable using (Library lib = new Library()) { String sOutput = "Indexed-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font; try { font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); } catch (ApplicationException ex) { if (ex.Message.Equals("The specified font could not be found.") && System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices .OSPlatform.Linux) && !System.IO.Directory.Exists("/usr/share/fonts/msttcore/")) { Console.WriteLine("Please install Microsoft Core Fonts on Linux first."); return; } throw; } ColorSpace baseCS = ColorSpace.DeviceRGB; List <Int32> lookup = new List <Int32>(); int[] lowhi = { 0, 255 }; foreach (int r in lowhi) { foreach (int g in lowhi) { foreach (int b in lowhi) { lookup.Add(r); lookup.Add(g); lookup.Add(b); } } } IndexedColorSpace cs = new IndexedColorSpace(baseCS, 7, lookup); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new[] { 4.0 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { Console.WriteLine("Clips Sample:"); using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); String sOutput = "../Clips-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Output file: " + sOutput); // Create a new document and blank first page Document doc = new Document(); Rect rect = new Rect(0, 0, 612, 792); Page page = doc.CreatePage(Document.BeforeFirstPage, rect); Console.WriteLine("Created new document and first page."); // Create a new path, set up its graphic state and PaintOp Path path = new Path(); GraphicState gs = new GraphicState(); Color color = new Color(0.0, 0.0, 0.0); gs.FillColor = color; path.GraphicState = gs; path.PaintOp = PathPaintOpFlags.Fill; // Add a rectangle to the path Point point = new Point(100, 500); path.AddRect(point, 300, 200); Console.WriteLine("Created new path and added rectangle to it."); // Add a curve to the path too so we have something // interesting to look at after clipping Point linePoint1 = new Point(400, 450); Point linePoint2 = new Point(350, 300); path.AddCurveV(linePoint1, linePoint2); Console.WriteLine("Added curve to the path."); // Add the path to the page in the document Content content = page.Content; content.AddElement(path); Console.WriteLine("Added path to page in document."); // Create a new path and add a rectangle to it Path clipPath = new Path(); point = new Point(50, 300); clipPath.AddRect(point, 300, 250); Console.WriteLine("Created clipping path and added rectangle to it."); // Create a new clip and add the new path to it and then add // this new clip to the original path as its Clip property Clip clip = new Clip(); clip.AddElement(clipPath); path.Clip = clip; Console.WriteLine("Created new clip, assigned clipping path to it, and added new clip to original path."); // Update the page's content and save the file with clipping page.UpdateContent(); doc.Save(SaveFlags.Full, sOutput); // Kill the doc object doc.Dispose(); Console.WriteLine("Killed document object."); } }
static void Main(string[] args) { Console.WriteLine("AddTextToImage Sample:"); // ReSharper disable once UnusedVariable using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); String sInput = Library.ResourceDirectory + "Sample_Input/text_as_image.jpg"; String sOutput = "AddTextToImage-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Console.WriteLine("Input file: " + sInput); Console.WriteLine("Writing output to: " + sOutput); OCRParams ocrParams = new OCRParams(); //The OCRParams.Languages parameter controls which languages the OCR engine attempts //to detect. By default the OCR engine searches for English. List <LanguageSetting> langList = new List <LanguageSetting>(); LanguageSetting languageOne = new LanguageSetting(Language.English, false); langList.Add(languageOne); //You could add additional languages for the OCR engine to detect by adding //more entries to the LanguageSetting list. //LanguageSetting languageTwo = new LanguageSetting(Language.Japanese, false); //langList.Add(languageTwo); ocrParams.Languages = langList; // If your image resolution is not 300 dpi, specify it here. Specifying a // correct resolution gives better results for OCR, especially with // automatic image preprocessing. // ocrParams.Resolution = 600; using (OCREngine ocrEngine = new OCREngine(ocrParams)) { //Create a document object using (Document doc = new Document()) { using (Image newimage = new Image(sInput, doc)) { // Create a PDF page which is the size of the image. // Matrix.A and Matrix.D fields, respectively. // There are 72 PDF user space units in one inch. Rect pageRect = new Rect(0, 0, newimage.Matrix.A, newimage.Matrix.D); using (Page docpage = doc.CreatePage(Document.BeforeFirstPage, pageRect)) { docpage.Content.AddElement(newimage); docpage.UpdateContent(); } } using (Page page = doc.GetPage(0)) { Content content = page.Content; Element elem = content.GetElement(0); Image image = (Image)elem; //PlaceTextUnder creates a form with the image and the generated text //under the image. The original image in the page is then replaced by //by the form. Form form = ocrEngine.PlaceTextUnder(image, doc); content.RemoveElement(0); content.AddElement(form, Content.BeforeFirst); page.UpdateContent(); } doc.Save(SaveFlags.Full, sOutput); } } } }
static void Main(string[] args) { // ReSharper disable once UnusedVariable using (Library lib = new Library()) { String sOutput = "CalGray-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font; try { font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); } catch (ApplicationException ex) { if (ex.Message.Equals("The specified font could not be found.") && System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices .OSPlatform.Linux) && !System.IO.Directory.Exists("/usr/share/fonts/msttcore/")) { Console.WriteLine("Please install Microsoft Core Fonts on Linux first."); return; } throw; } // a space consisting of the Y dimension of the CIE 1931 XYZ // space with the CCIR XA/11-recommended D65 white point and // opto-electronic transfer function. Double[] whitePoint = { 0.9505, 1.0000, 1.0890 }; Double[] blackPoint = { 0.0, 0.0, 0.0 }; double gamma = 2.2222; ColorSpace cs = new CalGrayColorSpace(whitePoint, blackPoint, gamma); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new[] { 0.5 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }
static void Main(string[] args) { Console.WriteLine("AddTextToImage Sample:"); using (Library lib = new Library()) { Console.WriteLine("Initialized the library."); String sInput = Library.ResourceDirectory + "Sample_Input/text_as_image.jpg"; String sOutput = "../AddTextToImage-out.pdf"; if (args.Length > 0) { sInput = args[0]; } if (args.Length > 1) { sOutput = args[1]; } Console.WriteLine("Input file: " + sInput); Console.WriteLine("Writing output to: " + sOutput); OCRParams ocrParams = new OCRParams(); // Setting the segmentation mode to AUTOMATIC lets the OCR engine // choose how to segment the page for text detection. ocrParams.PageSegmentationMode = PageSegmentationMode.Automatic; // This tells the selected engine to improve accuracy at the expense // of increased run time. For Tesseract 3, it runs two different // algorithms, and chooses the one that has the most confidence. ocrParams.Performance = Performance.BestAccuracy; using (OCREngine ocrEngine = new OCREngine(ocrParams)) { //Create a document object using (Document doc = new Document()) { using (Image newimage = new Image(sInput, doc)) { // Create a PDF page which is the size of the image. // Matrix.A and Matrix.D fields, respectively. // There are 72 PDF user space units in one inch. Rect pageRect = new Rect(0, 0, newimage.Matrix.A, newimage.Matrix.D); using (Page docpage = doc.CreatePage(Document.BeforeFirstPage, pageRect)) { docpage.Content.AddElement(newimage); docpage.UpdateContent(); } } using (Page page = doc.GetPage(0)) { Content content = page.Content; Element elem = content.GetElement(0); Image image = (Image)elem; //PlaceTextUnder creates a form with the image and the generated text //under the image. The original image in the page is then replaced by //by the form. Form form = ocrEngine.PlaceTextUnder(image, doc); content.RemoveElement(0); content.AddElement(form, -1); page.UpdateContent(); } doc.Save(SaveFlags.Full, sOutput); } } } }
static void Main(string[] args) { // ReSharper disable once UnusedVariable using (Library lib = new Library()) { String sOutput = "DeviceN-out.pdf"; if (args.Length > 0) { sOutput = args[0]; } Console.WriteLine("Writing to output " + sOutput); Document doc = new Document(); Page page = doc.CreatePage(Document.BeforeFirstPage, new Rect(0, 0, 5 * 72, 4 * 72)); Content content = page.Content; Font font; try { font = new Font("Times-Roman", FontCreateFlags.Embedded | FontCreateFlags.Subset); } catch (ApplicationException ex) { if (ex.Message.Equals("The specified font could not be found.") && System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices .OSPlatform.Linux) && !System.IO.Directory.Exists("/usr/share/fonts/msttcore/")) { Console.WriteLine("Please install Microsoft Core Fonts on Linux first."); return; } throw; } ColorSpace alternate = ColorSpace.DeviceRGB; Double[] domain = { 0.0, 1.0, 0.0, 1.0 }; Double[] range = { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 }; string code = "{ 0 exch }"; Function tintTransform = new PostScriptCalculatorFunction(domain, range, code); ColorSpace cs = new DeviceNColorSpace(new[] { "DLRed", "DLBlue" }, alternate, tintTransform); GraphicState gs = new GraphicState(); gs.FillColor = new Color(cs, new[] { 0.75, 0.75 }); Matrix textMatrix = new Matrix(24, 0, 0, 24, // Set font width and height to 24 point size 1 * 72, 2 * 72); // x, y coordinate on page, 1" x 2" TextRun textRun = new TextRun("Hello World!", font, gs, new TextState(), textMatrix); Text text = new Text(); text.AddRun(textRun); content.AddElement(text); page.UpdateContent(); doc.EmbedFonts(); doc.Save(SaveFlags.Full, sOutput); } }