We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to resize PDFLoadedDocument without losing its editable fields?

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

3 Replies

SP Selvakumar P Syncfusion Team January 24, 2011 09:57 AM UTC

Hi Kim,

Thank you for using Syncfusion Products.

In Essential Pdf, while creating Pdf template from the page of a document, we do not import the interactive features such as form fields etc.. and We just import the contents from the page.

Please refer to the sample in the link below for the same.

Sample:
97973-2093017423.zip

Please let us know if you have any questions.

Regards,
Selvakumar.



KI kim January 24, 2011 01:24 PM UTC

Hi Selvakumar!

Thanks for response, but that was not my question.

My question was:

"How do I resize the PdfLoadedDocument without losing the editable fields?"

The code I gave you just showed you the way I resized the PdfLoadedDocument, and it did not work.

I don't know if you know that the fields get screw up when you try to use DrawPdfTemplate.(Compare INPUT and OUTPUT. you don't see Text1 and Text2 anymore. You see only Page3Text3 => and the problem is the Pdf has Form fields, and I think that is a bug)

I would like to ask my question again:

How do I resize the PdfLoadedDocument without losing the editable fields?

Thanks!



SP Selvakumar P Syncfusion Team January 25, 2011 05:10 AM UTC

Hi Kim,

Thanks for the response. As stated in the previous response, we cannot import the interactive features such as annotations, form fields etc... to the Pdf Template. So, obviously when we draw the Pdf Template, form fields are not added in this case. So, we cannot resize the loaded document without losing the editable fields.

Let us know if you have any questions.

Regards,
Selvakumar.



Loader.
Live Chat Icon For mobile
Up arrow icon