Created
October 8, 2013 22:46
-
-
Save duncansmart/6893090 to your computer and use it in GitHub Desktop.
Use EO.PDF to add form fieldst o a PDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//File.WriteAllText(@"c:\temp\before.htm", html); | |
// Ensure all inputs have an ID | |
var htmlDoc = new HtmlAgilityPack.HtmlDocument(); | |
htmlDoc.OptionWriteEmptyNodes = true; | |
htmlDoc.LoadHtml(html); | |
var srcInputs = htmlDoc.DocumentNode.SelectNodes("//input"); | |
foreach (var item in srcInputs) | |
{ | |
if (item.Id.Trim().Length == 0) | |
item.SetAttributeValue("id", Guid.NewGuid().ToString("N")); | |
} | |
var sw = new StringWriter(); | |
htmlDoc.Save(sw); | |
html = sw.ToString(); | |
//File.WriteAllText(@"c:\temp\after.htm", html); | |
// Convert to PDF | |
PdfDocument pdf = new PdfDocument(); | |
var conversion = EO.Pdf.HtmlToPdf.ConvertHtml(html, pdf, pdfOptions); | |
// Find <input>s and overlay PDF controls | |
var inputs = conversion.HtmlDocument.GetElementsByTagName("INPUT"); | |
int i = 0; | |
foreach (HtmlElement input in inputs) | |
{ | |
Debug.WriteLine(input.Name + ": '" + input.ClassName + "'"); | |
AcmContent acmContent = null; | |
var srcInput = srcInputs.Single(x => x.Id == input.ID); | |
var inputType = srcInput.GetAttributeValue("type", "text"); | |
if (inputType == "text") | |
{ | |
var txt = new AcmTextBox(); | |
acmContent = txt; | |
txt.Name = input.Name ?? input.ID; | |
txt.Style.OffsetX = input.Location.X; | |
txt.Style.OffsetY = input.Location.Y; | |
txt.Style.Height = input.Size.Height; | |
txt.Style.Width = input.Size.Width; | |
} | |
else if (inputType == "radio") | |
{ | |
var chk = new AcmCheckBox(); | |
acmContent = chk; | |
chk.Style.OffsetX = input.Location.X - 0.1f; | |
chk.Style.OffsetY = input.Location.Y; | |
chk.Style.Height = input.Size.Height; | |
chk.Style.Width = input.Size.Width; | |
chk.Style.Border = new AcmBorder(new AcmLineInfo(Color.Black, 0.01f)); | |
chk.Name = (input.Name ?? input.ID) + ":" + i; | |
} | |
var render = new AcmRender(input.Location.Page, 0, new AcmPageLayout(new AcmPadding(0, 0, 0, 0))); | |
render.Render(acmContent); | |
i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment