Print document in Grayscale, Trim Embedded fonts to Subset, Rotate Text, determine blank pages and optimized PDF/A generation using Aspose.Pdf for .NET 17.5

The new release of Aspose.Pdf for .NET 17.5 has been published with exciting new features, some minor enhancements and fixes related to bugs reported in earlier release versions. In every new release, we try our best to publish a better, stable and exciting release which can facilitate our customers and help them to accomplish great features in less amount of time. The ease of use and rich support contents including product support documentation are among the salient features of our API’s and we always strive to enrich the programmers guide with detailed articles explaining steps to use new and existing features of APIs. This new release version also contains some promising features which were recently requested by our customers.

Print a Document as Grayscale

PdfViewer class in Aspose.Pdf.Facades offers the feature to print the PDF documents. We can print the file to default attached printer or any other printer already configured with the system and during file printing, the API prints the file in default colors. However we recently received a requirement to print the file in Grayscale and in order to accomplish this requirement, we have introduced PrintAsGrayscale property in PdfViewer class. The following code snippet can be used to print the file in Grayscale.

using (Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer())
{
    string outputFile = @"c:\41686.out.xps";

    viewer.OpenPdfFile(inputFile);
    viewer.PrintPageDialog = false;
    viewer.PrintAsGrayscale = true;

        System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
        System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();

    ps.PrinterName = "Microsoft XPS Document Writer";
    ps.PrintFileName = outputFile;
    ps.PrintToFile = true;
    pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
    ps.DefaultPageSettings.PaperSize = pgs.PaperSize;
    viewer.PrintDocumentWithSettings(pgs, ps);
}
Another option can be to Convert a PDF from RGB colorspace to Grayscale and simply call PDF printing routine.

Trim fully embed fonts to subsets only

Aspose.Pdf for .NET provides the capabilities of Working with Text elements. Some of the salient features include Addition Text to PDF file, Extraction of Text from PDF file, Searching and getting Text from PDF file and Replacement of Text inside the PDF document. During PDF file creation, we can specify a custom font to be used inside the document and this feature can be accomplished while using TextState.Font property of TextFragment object which takes FontRepository.FindFont(“TimesNewRoman”); object as an argument. The font name which we want to use during PDF creation can be provided as an argument to FindFont(…) method. When using custom font inside PDF file, we need to either Embed it or add it’s subset so that when viewing the document on system which do not have font used inside PDF document, the layout is not disturbed. In order to embed the font inside PDF document, we need to set the value of TextFragment.TextState.Font.IsEmbedded property as True and in order to add subset of custom font inside PDF file, we need to set the value of TextFragment.TextState.Font.IsSubset to True.

However please note that when we add the font as Embedded resource inside PDF file, the complete font is included in document and the size of document may become larger. However when using Subset of font, only the glyph of font used inside the document is embedded, rather than complete font.

Aspose.Pdf for .NET provides variety of features to deal with fonts in PDF document. Some of the salient features include

If we open any PDF document in Adobe Acrobat/Reader, and check PDF file properties using File -> Properties menu option, when we click on the “Fonts” tab, we can see fonts that have been embedded inside the document. Some may be fully embedded (“Embedded”) and others may be partially embedded (“Embedded Subset”). Apart from above mentioned capabilities, we may come across a requirement to take fully embedded font sets and trim them down to subsets only.

Currently Aspose.Pdf for .NET controls font subsetting declared in interface Document.IDocumentFontUtilities. Every object of type Aspose.Pdf.Document has property FontUtilities of type IDocumentFontUtilities. This interface includes method SubsetFonts(FontSubsetStrategy subsetStrategy). The Parameter subsetStrategy helps to tune subset strategy and currently two variants of font subsetting are supported. At the moment, it is possible to subset all fonts used by document(strategy SubsetAllFonts) or to subset only fully embedded fonts(or font subsets which are larger than it required by current document).

  • SubsetAllFonts – All the fonts will be embedded into document as font subset is embedded into PDF document by definition
  • SubsetEmbeddedFontsOnly – This process will not affect fonts which are not embedded into document.

The following code snippet can be used to reduce fully embedded font sets to only those subsets that are used:

Document doc = new Document(dataDir + "input.pdf");
// All fonts will be embedded as subset into document in case of SubsetAllFonts.
doc.FontUtilities.SubsetFonts(Document.FontSubsetStrategy.SubsetAllFonts);
// Font subset will be embedded for fully embedded fonts but fonts which are not embedded into document will not be affected.
doc.FontUtilities.SubsetFonts(Document.FontSubsetStrategy.SubsetEmbeddedFontsOnly);
doc.Save(dataDir + "Output_out.pdf");

Rotation of TextFragment

We can add a text inside PDF file using TextFragment or TextBuilder objects. When using TextBuilder, we simply need to add TextFragments to TextBuilder object using AppendText(..) method and we do not need to exclusively add TextBuilder object to Paragraphs collection of page instance. However when only using TextFragment object, we need to add it to paragraphs collection of Page instance. Recently we received a requirement to rotate the text inside PDF document. In order to accomplish this requirement, we have added a Rotation property to TextParagraph and TextFragments objects. It expects value of rotation angle specified in degrees and will work in PDF document generation scenarios. Specified below is the code snippet to rotate text when using TextFragment and TextBuilder objects.

//open document
Document pdfDocument = new Document();
//get particular page
Page pdfPage = (Page)pdfDocument.Pages.Add();

//create text fragment
TextFragment textFragment1 = new TextFragment("main text");
textFragment1.Position = new Position(100, 600);

//set text properties
textFragment1.TextState.FontSize = 12;
textFragment1.TextState.Font = FontRepository.FindFont("TimesNewRoman");

//create rotated text fragment
TextFragment textFragment2 = new TextFragment("rotated text");
textFragment2.Position = new Position(200, 600);

//set text properties
textFragment2.TextState.FontSize = 12;
textFragment2.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment2.TextState.Rotation = 45;

//create rotated text fragment
TextFragment textFragment3 = new TextFragment("rotated text");
textFragment3.Position = new Position(300, 600);

//set text properties
textFragment3.TextState.FontSize = 12;
textFragment3.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment3.TextState.Rotation = 90;

// create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
// append the text fragment to the PDF page
textBuilder.AppendText(textFragment1);
textBuilder.AppendText(textFragment2);
textBuilder.AppendText(textFragment3);
//save document
pdfDocument.Save("TextFragmentTests_Rotated1.pdf");

For further details on supporting same feature with various approaches, please visit Rotate Text Inside PDF

Detect blank pages

There are scenarios where PDF documents may contain blank pages and customers may have a requirement to determine the blank pages inside the document. One of the approach is to iterate through each page of the document, identify any images and determine if the images have white color or different colors, try parsing text content and determine the length of extracted text and every-time we need to write the complete code snippet. However we have introduced a build in feature in API to accomplish this requirement. The IsBlank(..) method of Page object can be used to fulfill this requirement where it takes an argument for threshold level.

string inputFileName = @"c:\sample.pdf";
// load existing PDF file
Document pdfDocument = new Document(inputFileName);
// determine if first page fo document is blank where we can define the threshold level
bool isBlank = pdfDocument.Pages[1].IsBlank(0.01d);
// determine if second page is blank or not
isBlank = pdfDocument.Pages[2].IsBlank(0.01d);

Miscellaneous fixes

Apart from above mentioned improvements, the PDF to Image conversion, Text extraction, Text replacement, PDF to Image conversion, PDF file signing, PDF to HTML, Rotation of table object, PDF form filling, PDF file merge and import of Annotations are also improvement areas. Its always recommended to use latest release of our API’s, so we suggest you to please download the latest release of Aspose.Pdf for .NET 17.5 and check Release Notes section for list of issues fixed in Aspose.Pdf for .NET 17.5


To keep up with our news, you can follow us on Twitter or follow our Facebook page.

Leave a Reply