We have introduced a new feature in Aspose.Pdf for .NET which allows developers to only restrict numeric values to be entered inside Form fields. In order to accomplish this requirement, OnModifyCharacter and OnFormat properties are added to Field.Actions. In order to add validation of user entry, AFNumber_Keystroke and AFNumber_Format java script functions may be used. Please take a look over following code snippet.
// load input PDF file
Document doc = new Document("c:/pdftest/ABFillablewfields.pdf");
TextBoxField field = (TextBoxField)doc.Form["husband name"];
//http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/FormsAPIReference.pdf
//2 digits after point
//no separator
//neg style = minus
//no currency
field.Actions.OnModifyCharacter = new JavascriptAction("AFNumber_Keystroke(2, 1, 1, 0, \"\", true)");
field.Actions.OnFormat = new JavascriptAction("AFNumber_Format(2, 1, 1, 0, \"\", true)");
// set initial field value
field.Value = "123";
// save resultant PDF
doc.Save("c:/pdftest/Restricted_out.pdf");
Aspose.Pdf for .NET provides great capabilities of converting PDF files to DOC, DOCX, Image, HTML and various other formats. During PDF to HTML conversion process, the images inside PDF are saved with SVG compression but you may consider saving them in raster format PNG/JPEG so that size of images is reduced.
// source PDF file
Document doc = new Document("c:/input.pdf");
// create HtmlSaveOption with tested feature
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.FixedLayout = true;
saveOptions.SplitIntoPages = false;
saveOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg;
string outFile = "c:/ResultantFile.pdf";
// save the output in HTML format
doc.Save(outFile, saveOptions);
Split CSS to pages during PDF to HTML conversion
HtmlSaveOptions class has property named SplitIntoPages which supports the feature split resultant HTML file to pages when generating the output. Recently one of the customers has the requirement to split the CSS file based on individual page instead of generating a single CSS file. In order to accomplish this requirement, we have introduced a new flag SplitCssIntoPages in HtmlSaveOptions class. When the value of this property is set to true, the converter will split resultant CSS into parts/pages based on individual HTML page created. Please take a look over following code snippet.
//1) clean-up target folders
string htmlFile = Path.GetFullPath("c:/pdftest/ResultantFile_files/" + "resultant.html");
string imagesDir = Path.GetDirectoryName(htmlFile) + @"\35942_files";
string cssDir = Path.GetDirectoryName(htmlFile) + @"\35942_css_files";
if (Directory.Exists(imagesDir)) { Directory.Delete(imagesDir, true); };
if (Directory.Exists(cssDir)) { Directory.Delete(cssDir, true); };
//2) create document for conversion
Document pdfDocument = new Document("c:/pdftest/antrag.pdf");
//3) tune conversion options
HtmlSaveOptions options = new HtmlSaveOptions();
options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsPngImagesEmbeddedIntoSvg;//<- to get compatibility with previous behavior and therefore same result of tests
// split HTML output into pages
options.SplitIntoPages = true;
// split css into pages
options.SplitCssIntoPages = true;
options.CustomCssSavingStrategy = new HtmlSaveOptions.CssSavingStrategy(Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY);
options.CustomStrategyOfCssUrlCreation = new HtmlSaveOptions.CssUrlMakingStrategy(Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING);
//3) do conversion
pdfDocument.Save(htmlFile, options);
private static void Strategy_4_CSS_MULTIPAGE_SAVING_RIGHT_WAY(HtmlSaveOptions.CssSavingInfo partSavingInfo)
{
string cssOutFolder = @"c:\pdftest\ResultantFile_files\";
if (!Directory.Exists(cssOutFolder))
{
Directory.CreateDirectory(cssOutFolder);
}
string outPath = cssOutFolder + "style_xyz_page" + partSavingInfo.HtmlPageNumber.ToString() + ".css";
System.IO.BinaryReader reader = new BinaryReader(partSavingInfo.ContentStream);
System.IO.File.WriteAllBytes(outPath, reader.ReadBytes((int)partSavingInfo.ContentStream.Length));
}
private static string Strategy_5_CSS_MAKING_CUSTOM_URL_FOR_MULTIPAGING(HtmlSaveOptions.CssUrlRequestInfo requestInfo)
{
return "/document-viewer/GetCss?cssId=4544554445_page{0}";
}
Determine field properties
During PDF form creation, some fields are marked as required/mandatory and we might come across a requirement to determine if any particular field is marked as required or not. Please try using the following code snippet to find if the field is marked as required or not.
// load source PDF file
Document pdf = new Document(@"c:\pdftest\RequiredField_output.pdf");
// instantiate Form object
Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(pdf);
// itterate through each field inside PDF form
foreach (Field field in pdf.Form.Fields)
{
// determine if the field is marked as required or not
bool isRequired = pdfForm.IsRequiredField(field.FullName);
if (isRequired)
{
// print either the field is marked as required or not
Console.WriteLine("The field named " + field.FullName + " is required");
}
}
Enhancements
We have made enhancements in numerous functionalities. The following section highlight some of the enhancements introduced in this release
- PDF to HTML: Extract Images,Fonts and CSS in separate folders and create their aliases
- Adding JavaScript to PushButton using Aspose.Pdf.Generator
- PDF to HTML – For non-vector SVG elements, convert them to PNG / JPG
- PDF to HTML – Shorten font names
- Mark Producer and Creator properties of DocumentInfo as Read Only
- PDF generation time is optimized as compare to previous releases
- Better PDF to XPS and Image rendering
- Get the chance to explore the new and exciting features of Aspose.Pdf for .NET 8.7.0.