Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Wednesday, January 26, 2022 7:32:44 AM(UTC)

Amin  
Amin

Groups: Manager, Tech Support
Posts: 367

Was thanked: 1 time(s) in 1 post(s)

The C# WinForms code below uses LEADTOOLS 22 DocumentWriter and DocumentWriterEmfPage classes to produce a PDF using the following inputs:


  1. Image that was previously processed with OCR.
  2. An XML file that contains the text from that image, in addition to the coordinates and size of every string.


The code produces a Windows enhanced metafile (EMF) and draws all the text string on it, each in its location, then uses that EMF along with the image in a DocumentWriterEmfPage and inserts the page into a PDF document.

If multiple pages are required, the process of creating the page and adding it can be repeated before calling DocumentWriter.EndDocument();

Code:


void MergeImageAndText()
{
   MemoryStream ms = new MemoryStream();
   var gTemp = CreateGraphics();
   Metafile mf = new Metafile(ms, gTemp.GetHdc());
   Graphics gEmf = Graphics.FromImage(mf);
   RasterCodecs codecs = new RasterCodecs();
   RasterImage img = codecs.Load("image.tif");
   gEmf.DrawRectangle(Pens.White, 0, 0, img.Width, img.Height);


   XmlDocument doc = new XmlDocument();
   doc.Load("text.xml");
   // Parsing below is specific to our XML
   // Obtain text contents, location and width to draw it into the EMF file
   foreach (XmlNode node in doc.DocumentElement.ChildNodes)
      foreach (XmlNode n3 in node)
         foreach (XmlNode n2 in n3)
            foreach (XmlNode n1 in n2)
               foreach (XmlNode n in n1)
                  foreach (XmlNode stringNode in n)
                     if (stringNode.Name == "String")
                     {
                        string Content = stringNode.Attributes["CONTENT"]?.InnerText;
                        float x = float.Parse(stringNode.Attributes["HPOS"]?.InnerText);
                        float y = float.Parse(stringNode.Attributes["VPOS"]?.InnerText);
                        float w = float.Parse(stringNode.Attributes["WIDTH"]?.InnerText);
                        Font font = SystemFonts.DefaultFont;
                        float w0 = gEmf.MeasureString(Content, font).Width - 4f;
                        float factor = w / w0;
                        font = new Font(font.FontFamily, font.Size * factor);
                        gEmf.DrawString(Content, font, Brushes.White, x, y);
                        font.Dispose();
                     }
   gEmf.Dispose(); // finished drawing the text

   // Use the EMF in the page with the loaded image on top of it
   DocumentWriterEmfPage page = new DocumentWriterEmfPage();
   page.Image = img;
   page.EmfHandle = mf.GetHenhmetafile();
   DocumentWriter docWriter = new DocumentWriter();
   PdfDocumentOptions pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
   pdfOptions.DocumentType = PdfDocumentType.PdfA;
   pdfOptions.ImageOverText = true;
   pdfOptions.DocumentResolution = img.XResolution;
   pdfOptions.EmptyPageResolution = img.XResolution;
   docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);
   docWriter.BeginDocument("merged.pdf", DocumentFormat.Pdf);

   docWriter.AddPage(page);
   docWriter.EndDocument();
   mf.Dispose();
   gTemp.ReleaseHdc();
   gTemp.Dispose();
}
Amin Dodin

Senior Support Engineer
LEAD Technologies, Inc.
LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.036 seconds.