LEADTOOLS Support
Document
Document SDK Examples
HOW TO: Create image-over-text PDF using text coordinates
#1
Posted
:
Wednesday, January 26, 2022 7:32:44 AM(UTC)
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:
- Image that was previously processed with OCR.
- 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.
LEADTOOLS Support
Document
Document SDK Examples
HOW TO: Create image-over-text PDF using text coordinates
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.