Hi there!
I tried to save a PDFLoadedDocument with editable fields (PdfLoadedForm); however, I had to resize (for mapping stuff) the PDF before I saved the PDF.
When I use the code below to save it (create a new PDF), the new PDF lost the all the editable fields (the Form = null).
How do I resize the PdfLoadedDocument without losing the editable fields?
Thanks!
public string ResizePDF(string inputPdfPath, float defaultHeight)
{
string saveToPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\MultPageForm.pdf";
if (File.Exists(inputPdfPath))
{
try
{
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputPdfPath);
float height = loadedDocument.Pages[0].Size.Height;
float ratio = defaultHeight / height;
//TEST HERE
try
{
PdfLoadedForm form = loadedDocument.Form;
foreach (object obj in form.Fields)
{
PdfLoadedTextBoxField tb = obj as PdfLoadedTextBoxField;
if (tb != null)
{
MessageBox.Show(tb.Name);
}
}
}
catch (Exception ex)
{
}
float newHeight = height * ratio;
float newWidth = loadedDocument.Pages[0].Size.Width * ratio;
System.Drawing.SizeF newSizeF = new System.Drawing.SizeF(newWidth, newHeight);
PdfDocument document = new PdfDocument();
document.PageSettings.Size = newSizeF;
document.PageSettings.Margins.All = 0;
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
PdfPage page = document.Pages.Add();
PdfGraphics g = page.Graphics;
PdfPageBase lpage = loadedDocument.Pages[i];
PdfTemplate template = lpage.CreateTemplate();
g.DrawPdfTemplate(template, System.Drawing.PointF.Empty, newSizeF);
}
{
PdfLoadedForm form = document.Form;
foreach (object obj in form.Fields)
{
PdfLoadedTextBoxField tb = obj as PdfLoadedTextBoxField;
if (tb != null)
{
MessageBox.Show(tb.Name);
}
}
}
catch (Exception ex)
{
}
document.Save(saveToPath);
document.Close(true);
loadedDocument.Close(true);
}
catch
{
//In case can not save or can not open existed file
saveToPath = string.Empty;
}
}
return saveToPath;
}
MultPageForm_8c22de52.rar