Nayyer Shahbaz October 22, 2014one Comment

PDF to PDF/A-2A Conversion, Set Output File Dimensions, Create Link to Already Attached Files with Aspose.Pdf for .NET 9.7.0

PDF to PDF/A-2A Conversion, Set Output File Dimensions, Create Link to Already Attached Files with Aspose.Pdf for .NET 9.7.0

October 22, 2014
Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Pdf for .NET logo We are very much excited to announce the release of Aspose.Pdf for .NET 9.7.0 which provides several great new features/enhancements and fixes for issues reported in earlier versions. Every new release is more powerful, reliable and performance optimized compared to previous versions. To benefit the latest features, we always encourage our customers to migrate to the latest release.

Create Internal Link to Already Attached Files

Aspose.Pdf for .NET supports the feature to add/update attachments inside PDF documents. It also offers the feature to create link annotations inside PDF files. The link can point to a page within the document, any other file placed on the system, a JavaScript link or some web location. However, the latest release also enables developers to create links to attachments already in the PDF file. For further information, please visit Add Hyperlink to Open Attachments in PDF.

// Load PDF document
Document doc = new Document("c:/pdftest/output_attach.pdf");
// Create attachment annoation
FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(0, 0, 16, 16), doc.EmbeddedFiles[1]);
// Add attachment to first page of PDF
// Set title for attachment
fileAttachment.Title = "Attached file";
// Save the PDF file

PDF to HTML – Set Output File Size (Dimensions)

HTML to PDF and PDF to HTML conversion are some of the main features that the development team are working to improve and in recent releases, we have made significant improvement to them. In the current release, we offer the new capability to set the output file dimensions when converting a PDF file to HTML format. The PdfPageEditor class present in the Aspose.Pdf.Facades namespace provides the feature to update/manipulate pages of PDF document. It also offers the feature to set/update page dimensions of input PDF file. In order to accomplish our requirement, we can first load the source PDF document into PdfPageEditor object, update page dimensions and then render the output in HTML format using HtmlSaveOptions class. For further details, please visit PDF to HTML – Set Output File Size.

// Select desirable page size
float newPageWidth = 400f;
float newPageHeight = 400f;

// Tune PdfPageEditor class 
Aspose.Pdf.Facades.PdfPageEditor pdfEditor = new Aspose.Pdf.Facades.PdfPageEditor();
// Bind source PDF file
// Set the page dimensions 
pdfEditor.PageSize = new Aspose.Pdf.PageSize(newPageWidth, newPageHeight);
// Set vertical alignment for page as center aligned
pdfEditor.VerticalAlignmentType = Aspose.Pdf.VerticalAlignment.Center;
// Set Horizontal alignment for page as center aligned
pdfEditor.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

// This scales page content to fit width,
// comment it out or set Zoom to 1.0F if You don't want to scale
// content and only want to change page's size(i.e. crop it)
float zoom = Math.Min((float)newPageWidth / (float)pdfEditor.Document.Pages[1].Rect.Width,
                    (float)newPageHeight / (float)pdfEditor.Document.Pages[1].Rect.Height);
pdfEditor.Zoom = zoom;// (float)595;

// Create stream object to hold file with updated dimensions
MemoryStream output = new MemoryStream();
// Save file to stream object

// Then reload scaled document and save it to HTML
Document exportDoc = new Document(output);
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
// This code shows page boreder in result - sometimes it comes in handy to see borders
SaveOptions.BorderPartStyle borderStyle = new SaveOptions.BorderPartStyle();
borderStyle.LineType = SaveOptions.HtmlBorderLineType.Dotted;
borderStyle.Color = System.Drawing.Color.Gray;
htmlOptions.PageBorderIfAny = new SaveOptions.BorderInfo(borderStyle);

// Conversion to HTML itself
exportDoc.Save(@"C:\pdftest\37240_Test1.html", htmlOptions);
// Close the stream object

PDF to PDF/A-2A Conversion

Aspose.Pdf for .NET has been supporting conversion from PDF to PDF/A compliant documents and also offers the capability to validate a PDF file’s PDF/A compliance. In this release, we have added support for conversion of PDF to PDF/A_2a format. Therefore a new value PDF_A_2A is added to PdfFormat enumeration. Please try using the following code snippet to accomplish this requirement.

// Load PDF file
Document doc = new Aspose.Pdf.Document("input.pdf");
// Set PDF/A compliance as PDF/A_1a
doc.Convert(new MemoryStream(), PdfFormat.PDF_A_2A, ConvertErrorAction.Delete);
// Save updated document

Add SVG Object to Table Cell

Aspose.Pdf for .NET supports the feature to add table cell inside PDF file. While creating table inside PDF file, we can add text or images inside table cells. Furthermore, this API also offers the feature to convert SVG files to PDF format. So with a blend of both features, we can accomplish the requirement of loading SVG image and add it inside table cells. For further details, please visit Add SVG Object to Table Cell.

// Instantiate Document object
Document doc = new Document();
// Create an image instance
Aspose.Pdf.Image img = new Aspose.Pdf.Image();
// Set image type as SVG
img.FileType = Aspose.Pdf.ImageFileType.Svg;
// Path for source file
img.File = "c:/pdftest/img_03.svg";
// Set width for image instance
img.FixWidth = 50;
// Set height for image instance
img.FixHeight = 50;
// Create table instance
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set width for table cells
table.ColumnWidths = "100 100";
// Create row object and add it to table instance
Aspose.Pdf.Row row = table.Rows.Add();
// Create cell object and add it to row instance
Aspose.Pdf.Cell cell = row.Cells.Add();
// Add textfragment to paragraphs collection of cell object
cell.Paragraphs.Add(new TextFragment("First cell"));
// Add another cell to row object
cell = row.Cells.Add();
// Add SVG image to paragraphs collection of recently added cell instance
// Create page object and add it to pages collection of document instance
Page page = doc.Pages.Add();
// Add table to paragraphs collection of page object
// Save PDF file

Set TextFragment Formatting as Underline

A PDF file consists of elements such as Text, Images, Attachments, Annotations, Hyperlinks, Tables etc. When adding Text string to PDF file, we may have a requirement to set formatting for text instance. The latest release of Aspose.Pdf for .NET 9.7.0 offers the feature to set Underline formatting for TextFragment when adding to PDF file. Please try setting TextState.Underline property as true to accomplish this requirement.

// Instantiate Document instance
Document pdfDocument = new Document();
// Add page to PDF file
// Create TextBuild object with first page as argument
TextBuilder tb = new TextBuilder(pdfDocument.Pages[1]);
// Create TextFragment instance with sample text
TextFragment fragment = new TextFragment("Test message");
// Set the font for TextFragment
fragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
// Set font size
fragment.TextState.FontSize = 10;
// Set text formatting as underline
fragment.TextState.Underline = true;
// Specify position for TextFragment
fragment.Position = new Position(10, 800);
// Add TextFragment to Text Builder instance
// Save the PDF file

Add Choice Option in Combobox Field

In case you need to add choice option to combo-box field in XFA form, then you need to update the XML template of the document so that the choice is added.

Document doc = new Document("37348.pdf");
string[] fields = doc.Form.XFA.FieldNames;
XmlNode node = doc.Form.XFA.GetFieldTemplate(fields[0]);
XmlNode items = node.SelectSingleNode("tpl:items", doc.Form.XFA.NamespaceManager);
XmlNode item = items.OwnerDocument.CreateNode(XmlNodeType.Element, "text", items.NamespaceURI);
item.InnerText = "Choice # 4 ( New )";

Among the above stated new features/enhancements, there are numerous other features introduced in this release. We have also made fixes for issues reported in earlier releases. Please get a chance to download and try using the latest release of Aspose.Pdf for .NET 9.7.0

Join the Conversation

1 Comment

Leave a comment

Posted inAspose.PDF Product Family, Nayyer Shahbaz

Related Articles