Convert a File to PDF Format

Convert XPS file to PDF format

To convert an XPS file to PDF using Aspose.PDF for Java, use XpsLoadOptions class. This is used to initialize a LoadOptions object. Later, this object is passed as an argument during the Document object initialization and helps the PDF rendering engine to determine the source document’s input format.

In both XP and Windows 7, you should find an XPS Printer pre-installed if you look in the Control Panel and then Printers. To create XPS files you can use that printer for the output device. In Windows 7, you should be able to just double-click the file to open it in an XPS viewer. You may also download XPS viewer from Microsoft’s website.

The following code snippet shows the process of converting the XPS file into PDF format.

Convert PCL file to PDF format

To allow conversion from PCL to PDF, Aspose.PDF for Java has the class PclLoadOptions which is used to initialize the LoadOptions object. This object is then passed as an argument during Document object initialization and helps the PDF rendering engine to determine the source document’s input format.

The following code snippet shows the process of converting a PCL file into PDF format.

Starting release of Aspose.PDF for Java 9.5.0, the API supports HP-GL to PDF conversion. So PCL with HP-GL can be converted to PDF using this approach.

PCL to PDF: Supported Features

Sets of CommandsSupportExceptionsDescription
Job control commands+Duplex printing modeControl print process: number of copies, output bin, simplex/duplex printing, left and top offsets etc.
Page control commands+Perforation Skip commandSpecify a size of page, margins, page orientation inter -lines, -character distances etc.
Cursor Positioning Commands+ Specify cursor position and, hence, origins of the text, raster or vector images and details.
Font selection commands+

1. Transparent Print Data Command.

2. Embedded soft fonts. In the current version instead of creating soft font our library selects suitable font from existing “hard” TrueType fonts installed on a target machine.
Suitability is defined by the width/height ratio.
This feature works only for Bitmap and TrueType fonts and does not guarantee that text printed with soft font will be relevant to the one in a source file.
Because character codes in the soft font can unmatched default ones.

3. User-Defined Symbol Sets.

Allow loading soft (embedded) fonts from the PCL file and managing them in memory.
Raster graphics commands+Only black & whiteAllow loading raster images from PCL file to memory, specify raster parameters
such as width, height, compression type, resolution, etc.
Color commands+ Allow coloring for all printable objects.
Print Model commands+ Allow filling text, raster images and rectangular areas with raster predefined and user-defined patterns, specify transparency mode for patterns and source raster image.
Predefined patterns are hatching, cross-hatch and shading ones.
Rectangle area fill commands+ Allow creation and filling rectangular areas with patterns.
HP-GL/2 Vector Graphics commands+Screened Vector Command (SV), Transparency Mode Command (TR), Transparent Data Command (TD), RO (Rotate Coordinate System), Scalable or Bitmap Fonts Command (SB), Character Slant Command (SL) and Extra Space (ES) are not implemented and DV (Define Variable Text Path) commands are realized in beta version.

- Allow loading HP-GL/2 vector images from the PCL file into memory. Vector image has an origin at the lower-left corner of the printable area, can be scaled, translated, rotated and clipped.

- A vector image can contain text, as labels, and geometric figures such as rectangle, circle, ellipse, line, arc, bezier curve and complex figures composed from the simple ones.

- Closed figures including letters of labels can be filled with solid fill or vector pattern.

- The pattern can be hatching, cross-hatch, shading, raster user-defined, PCL hatching or cross-hatch and PCL user-defined. PCL patterns are raster. Labels can be individually rotated, scaled, and directed in four directions: up, down, left and right. Left and Right directions involve one-after-another letter arrangement. Up and Down directions involve one-under-another letter arrangement.

Macross Allow loading a sequence of PCL commands into memory and use this sequence many times, for example, to print page header or set one formatting for a set of pages.
Unicode text Allow printing non-ASCII characters. Not implemented due to lack of sample files with
Unicode text
PCL6 (PCL-XL) Realized only in the Beta version because of lack in test files. Embedded fonts also are not supported. The JetReady extension is not supported because it is impossible to have JetReady specification.Binary file format.

PCL to PDF: Known Issues

  1. Origin of text strings and images can slightly differ from the ones in a source PCL file if the print direction is not 0º. The same refers to vector images if coordinate system of vector plot is rotated (RO command preceded).
  2. Origin of labels in vector images can be differ from the ones in a source PCL file if the labels are influenced by a sequence of commands: Label Origin (LO), Define Variable Text Path (DV), Absolute Direction (DI) or Relative Direction (DR).
  3. A text can be incorrectly read if it must be rendered with Bitmap or TrueType soft (embedded) font, because currently these fonts are only partially supported (see exceptions in “Supported features table”). In this situation text can be correctly read only if character codes in a soft font corresponds to default ones. A style of the read text also can be differed from the one in source PCL file because it is not necessary to set style in soft font header.
  4. If parsed PCL file contains Intellifont or Universal soft fonts, an exception will be thrown, because Intellifont and Universal font are not supported at all.
  5. If parsed PCL file contains macros commands the result of parsing will strongly differ from the source file, because macros commands are not supported.

Convert PostScript to PDF Format

In order to convert a PostScript file to PDF format, Aspose.PDF for Java offers PsLoadOptions class which is used to initialize the LoadOptions object. Later this object can be passed as an argument to Document object constructor, which will help PDF Rendering Engine to determine the format of source document. Following code snippet can be used to convert a PostScript file into PDF format:

Convert XSL-FO file to PDF format

Conversion of an XML document tree into various supported output formats, is a common need in applications and usually it’s a two-step process:

  1. Convert an XML source to an XSL-FO representation.
  2. Convert an XSL-FO representation to the target format.

This transformation allows the structure of the outer tree to be different from the structure of the original tree. While constructing the result tree, the tree transformation process may also add the information necessary to make additional formatting for result data. As an example, a table-of-contents may be added as a filtered selection of an original document, or source data rearrange into a numbered tabular view.

Please take a look at the following simple contents for XSL-FO to PDF conversion using Aspose.PDF for Java.

XSL-FO

 <?xml version="1.0" encoding="utf-8"?>

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <fo:layout-master-set>

    <fo:simple-page-master master-name="only"

                  page-height="29.7cm"

                  page-width="21cm"

                  margin-top="1cm"

                  margin-bottom="2cm"

                  margin-left="2.5cm"

                  margin-right="2.5cm">

      <fo:region-body

            margin-top="3cm" margin-bottom="1.5cm"

            margin-left="2cm" margin-right="2cm"/>

      <fo:region-before precedence="true" extent="3cm"/>

      <fo:region-after precedence="true" extent="1.5cm"/>

      <fo:region-start extent="1cm"/>

      <fo:region-end extent="1cm"/>

    </fo:simple-page-master>

  </fo:layout-master-set>

  <fo:page-sequence master-reference="only" initial-page-number="1">

    <fo:static-content flow-name="xsl-region-before">

      <fo:block text-align="end"

            font-size="10pt"

            font-family="serif"

            line-height="14pt" >

        XML Recommendation - p. <fo:page-number/>

      </fo:block>

    </fo:static-content>

    <fo:static-content flow-name="xsl-region-after">

      <fo:block text-align="center"

            font-size="10pt"

            font-family="serif"

            line-height="14pt" >

        After

      </fo:block>

    </fo:static-content>

    <fo:static-content flow-name="xsl-region-start">

      <fo:block-container border-color="black" border-style="solid" border-width="1pt"

      height="22.2cm" width="1cm" top="0cm" left="0cm" position="absolute">

      <fo:block text-align="start" font-size="8pt"

      font-family="serif" line-height="10pt">Start</fo:block>

    </fo:block-container>

    </fo:static-content>

    <fo:static-content flow-name="xsl-region-end">

      <fo:block-container border-color="black" border-style="solid" border-width="1pt"

      height="22.2cm" width="1cm" top="0cm" left="0cm" position="absolute">

      <fo:block text-align="start" font-size="8pt"

      font-family="serif" line-height="10pt">End</fo:block>

    </fo:block-container>

    </fo:static-content>

    <fo:flow flow-name="xsl-region-body">

      <fo:block font-size="18pt"

            font-family="sans-serif"

            line-height="24pt"

            space-after.optimum="15pt"

            background-color="blue"

            color="white"

            text-align="center"

            padding-top="0pt">

        Extensible Markup Language (XML) 1.0

      </fo:block>

      <fo:block font-size="16pt"

            font-family="sans-serif"

            line-height="20pt"

            space-before.optimum="10pt"

            space-after.optimum="10pt"

            text-align="start"

            padding-top="0pt">

        Abstract

      </fo:block>

      <fo:block font-size="12pt"

                font-family="sans-serif"

                line-height="15pt"

                space-after.optimum="3pt"

                text-align="start">

        The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to

        enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML

        has been designed for ease of implementation and for interoperability with both SGML and HTML. For further information

        go to <fo:basic-link external-destination="normal.pdf">normal.pdf</fo:basic-link>

      </fo:block>

      <fo:block font-size="16pt"

            font-family="sans-serif"

            line-height="20pt"

            space-before.optimum="10pt"

            space-after.optimum="10pt"

            text-align="start"

            padding-top="0pt">

        Status of this document

      </fo:block>

      <fo:block font-size="12pt"

                font-family="sans-serif"

                line-height="15pt"

                space-after.optimum="3pt"

                text-align="start">

        This document has been reviewed by W3C Members and other interested parties and has been endorsed by the Director as a

        W3C Recommendation. It is a stable document and may be used as reference material or cited as a normative reference from

        another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its

        widespread deployment. This enhances the functionality and interoperability of the Web. For further information go to

        <fo:basic-link external-destination="normal.pdf">normal.pdf</fo:basic-link>

      </fo:block>

      <fo:block font-size="12pt"

                font-family="sans-serif"

                line-height="15pt"

                space-after.optimum="3pt"

                text-align="start">

        This document specifies a syntax created by subsetting an existing, widely used international text processing standard

        (Standard Generalized Markup Language, ISO 8879:1986(E) as amended and corrected) for use on the World Wide Web. It

        is a product of the W3C XML Activity.

      </fo:block>

    </fo:flow>

  </fo:page-sequence>

</fo:root>

To convert the above specified XSL-FO file into PDF format, load the source XSL file to the Document object and pass XslFoLoadOptions as second object to the Document constructor:

Preview of the PDF document generated after XSL-FO conversion

todo:image_alt_text
Figure:XSL-FO file to PDF

Convert SVG file to PDF format

To convert SVG files to PDF, use the class named SvgLoadOptions which is used to initialize the LoadOptions object. Later, this object is passed as an argument during the Document object initialization and helps the PDF rendering engine to determine the input format of the source document.

The following code snippet shows the process of converting SVG file into PDF format.

We converted this tiger.svg to PDF.

A complex SVG graphic

todo:image_alt_text
Figure: SVG Tiger

SVG to PDF: Supported Features

SVG TagSample Use
circle<circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"/>
defs <defs>
<rect id="r1" width="15" height="15" stroke="blue" stroke-width="2" /> 
<circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"/> 
<circle id="r3" cx="10" cy="10" r="10" stroke="blue" stroke-width="3"/> 
</defs> 
<use x="25" y="40" xlink:href="#r1" fill="red"/> 
<use x="35" y="15" xlink:href="#r2" fill="green"/> 
<use x="58" y="50" xlink:href="#r3" fill="blue"/>
ecllipse <ellipse cx="2.5" cy="1.5" rx="2" ry="1" fill="red" />
font<text x="10" y="100" font-size="15" fill="red" > 
    Sample text 
</text>
<g fill="none" stroke="dimgray" stroke-width="1.5">
<line x1="-7" y1="-7" x2="-3" y2="-3"/>
<line x1="7" y1="7" x2="3" y2="3"/> 
<line x1="-7" y1="7" x2="-3" y2="3"/>
<line x1="7" y1="-7" x2="3" y2="-3"/>
</g>
image<image id="ShadedRelief" x="24" y="4" width="64" height="82" xlink:href="relief.jpg" />
line<line style="stroke:#eea;stroke-width:8" x1="10" y1="30" x2="260" y2="100"/> 
path<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/>
polygon<polygon style="stroke:#24a;stroke-width:1.5;fill:#eefefe" points="10,10 180,10 10,250 10,10" />
polyline<polyline fill="none" stroke="dimgray" stroke-width="1" points="-3,-6 3,-6 3,1 5,1 0,7 -5,1 -3,1 -3,-5"/>
rect <rect x="0" y="0" width="400" height="600" stroke="none" fill="aliceblue" />
style<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/>
svg<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="10cm" height="5cm" >
text<text font-family="sans-serif" fill="dimgray" font-size="22px" font-weight="bold" x="58" y="30" pointer-events="none">Map Title</text>
tref<defs>
<text id="ReferencedText">
Referenced character data
</text>
</defs>
<text x="10" y="100" font-size="15" fill="red" >
<tref xlink:href="#ReferencedText"/>
</text>
tspan<tspan dy="25" x="25"> six ink color input value.Here it will</tspan>
use<defs>
<text id="Text" x="400" y="200"
font-family="Verdana" font-size="100" text-anchor="middle" >
Masked text
</text>
<use xlink:href="#Text" fill="blue" />
SVG Tag PropertySample Use
fill<circle cx="120" cy="50" r="40" stroke="green" stroke-width="2" fill="magenta"/>
fill-opacity<path id="Blue50" d="M 0 125 L 100 125 L 100 225 z" fill="#00ffff" fill-opacity=".5" />
font-stretch<text x="10" y="20" font-family="arial" font-size="15" fill="blue" font-stretch="normal" >
font-stretch normal 
</text>
font-weight<text font-family="sans-serif" fill="dimgray" font-size="22px" font-weight="bold" x="58" y="30" pointer-events="none">Map Title</text>
opacity<circle id="a" r="30" fill="url(#g1)" stroke="black" opacity="0.5" />
stroke-dasharray<path d="M 50 50 L 100 50 L 100 100 L 150 100 L 150 50 L 200 50"
fill="none" stroke="red" stroke-width="2"
stroke-dashoffset="0" stroke-dasharray="1 2 3 4 5 6"  />
stroke-dashoffset<text stroke="black" y="1em"
stroke-dashoffset="1%"
stroke-dasharray="1px">
Sample text
</text>
stroke-linecap<circle cx="100" cy="100" r="0.00001" stroke="black" stroke-width="20" stroke-linecap="round"/>
stroke-linejoin<svg stroke-linecap="square" stroke-miterlimit="10" stroke-opacity="1" rendering="auto" fill="black" stroke-dasharray="none" stroke-width="1" stroke-linejoin="miter"  stroke-dashoffset="0">
stroke-opacity<line x1="75%" x2="75%" y2="100%" stroke="url(#red)" stroke-width="50%" stroke-opacity="0"/>
text-decoration<text x="10" y="20" word-spacing="0">Sample text</text>
<text x="10" y="45" text-decoration="underline">text-decoration underline</text> 
<text x="10" y="70" text-decoration="line-through">text-decoration line-through</text> 
<text x="10" y="95" text-decoration="overline">text-decoration overline
word-spacing<text x="10" y="20" word-spacing="0">Sample text </text>
<text x="10" y="45" word-spacing="20">Word space 20</text>
<text x="10" y="70" word-spacing="40">Word space 40</text>
<text x="10" y="95" word-spacing="0">Sample text</text>
SVG Tag AttributeSample Use
transform<text id="myScaleTextH" font-family="sans-serif" fill="dimgray" font-size="15px" text-anchor="middle" transform="translate(55,36),rotate(90)" >81,000 m</text>
cx<circle cx="100" cy="50" r="40" stroke="green" stroke-width="2" fill="magenta"/>
cy<circle fill="dimgray" cx="0" cy="0" r="1" pointer-events="none"/>
dx<svg xmlns="http://www.w3.org/2000/svg">  <br><text dx="㾫"/>  <br></svg>
dy<tspan dy="25" x="25">fallback to an icc-color or RGB color.</tspan>
fill<text x="10" y="20" fill="green">SVG Image 20</text>
font-family<tspan x="10" dy="10" font-family="serif"> 
Have you gazed on naked grandeur where there’s nothing else to gaze on, 
</tspan>
font-size<text font-family="sans-serif" fill="dimgray" font-size="22px" font-weight="bold" x="58" y="30" pointer-events="none">Map Title</text>
font-stretch<text x="10" y="20" font-family="arial" font-size="15" fill="blue" font-stretch="normal" > 
font-stretch normal 
<text> 

<text x="10" y="40" font-family="arial" font-size="15" fill="blue" font-stretch="wider" > 
font-stretch wider 
</text> 

<text x="10" y="60" font-family="arial" font-size="15" fill="blue" font-stretch="narrower" > 
font-stretch narrower 
</text>
font-style<text x="20" y="40" font-family="Arial" font-size="12" font-style="normal"> 
Sample text 
</text>
height<rect x="-6" y="-6" width="12" height="12" fill="white" stroke="dimgray" stroke-width="1.5"/>
id<defs> 
<text id="ReferencedText"> 
Referenced character data 
</text> 
</defs> 
<text x="10" y="100" font-size="15" fill="red" > 
<tref xlink:href="#ReferencedText"/> 
</text>
points<polyline fill="none" stroke="dimgray" stroke-width="1" points="-3,-6 3,-6 3,1 5,1 0,7 -5,1 -3,1 -3,-5"/>
r<circle cx="120" cy="50" r="40" stroke="green" stroke-width="2" fill="magenta"/>
rx<ellipse stroke="black" stroke-width="5" cx="400" cy="200" rx="350" ry="150" />
ry<rect x="20" y="20" rx="20" ry="20" width="250" height="100" style="fill:red;stroke:black; stroke-width:5;opacity:0.5"/>
style<rect x="20" y="20" width="250" height="250" style="fill:blue;stroke:red;stroke-width:5; fill-opacity:0.5;stroke-opacity:0.5;"/>
transform<circle cx="6cm" cy="2cm" r="100" fill="red" transform="translate(0,50)" />
width<rect style="stroke:#2aa;stroke-width:7;fill:#ded;opacity:.8" x="30" y="80" height="120" width="220" />
x<text x="10" y="20" fill="green">SVG Image 20</text>
y<text x="10" y="50" fill="green">SVG Image 50</text>
y1<line style="stroke:#eea;stroke-width:8" x1="10" y1="30" x2="260" y2="100"/>
y2<line x1="0" y1="0" x2="30" y2="30" style="stroke:rgb(99,99,99);stroke-width:2"/>

SVG to PDF: Known Issues

  1. Shorten path strings that not covered by specification but as I checked, rendered in browser fine
  2. Elliptical arc segments with transformations evolve complex 2D geometry calculations, require more testing.
  3. Big SVG files (megabytes size) take more than a few seconds to parse, code optimization required.
  4. Graphical filters API in the Pdf layer was extended recently, this makes possible to make more full integration of the corresponding SVG layer graphics functions.

Convert TeX file to PDF format

The LaTeX file format is a text file format with markup in the LaTeX derivative of the TeX family of languages and LaTeX is a derived format of the TeX system. LaTeX (ˈleɪtɛk/ lay-tek or lah-tek) is a document preparation system and document markup language. It is widely used for the communication and publication of scientific documents in many fields, including mathematics, physics, and computer science. It also has a prominent role in the preparation and publication of books and articles that contain complex multilingual materials, such as Sanskrit and Arabic, including critical editions. LaTeX uses the TeX typesetting program for formatting its output and is itself written in the TeX macro language.

Aspose.PDF for Java supports the feature to convert TeX files to PDF format and in order to accomplish this requirement, com.aspose.pdf package has a class named LatexLoadOptions which provides the capabilities to load LaTex files and render the output in PDF format using the Document class. The following code snippet shows the process of converting LaTex file to PDF format.

Java

 //Instantiate Latex Load option object

LatexLoadOptions Latexoptions = new LatexLoadOptions();

//Create Document object

com.aspose.pdf.Document doc = new com.aspose.pdf.Document("samplefile.tex", Latexoptions);

//Save the output in PDF file

doc.save("TeXToPDF_out.pdf");

Convert Text file to PDF format

Aspose.PDF for Java provides the capability to convert Text files to PDF format. In this article, we demonstrate how easily and efficiently we can convert a text file to PDF using Aspose.PDF.

When you need to convert a Text file to PDF, initially read the source text file in some reader. We have used StringBuilder to read the Text file contents. Instantiate Document object and add a new page in the Pages collection. Create a new object of TextFragment and pass StringBuilder object to its constructor. Add a new paragraph in Paragraphs collection using TextFragment object and save the resultant PDF file using the Save method of Document class.

Convert EPUB file to PDF format

In order to convert EPUB files to PDF format, Aspose.PDF for Java has a class named EpubLoadOptions which is used to load source EPUB file. After that, the object is passed as an argument to Document object initialization, as it helps the PDF rendering engine to determine the source document’s input format.

The following code snippet shows the process of converting an EPUB file into PDF format.

Convert XML file to PDF format

Aspose.PDF for Java provides the opportunity to convert an XML file into PDF document requiring that the input XML file must follow the Aspose.PDF for Java Schema.

Access TextFragement and TextSegment elements from XML file

The bindXML(..) method offers the feature to load XML file contents and Document.save() method is used to save the output in PDF format. However during conversion, we can also access individual elements inside XML template using XML id property with getObjectById() method. The following code snippet shows the steps to access TextSegments from the XML file.

XML

 <?xml version="1.0" encoding="utf-8" ?>

<Document xmlns="Aspose.PDF">

  <Page id="mainSection">

    <TextFragment>

      <TextSegment id="boldHtml">segment1</TextSegment>

    </TextFragment>

    <TextFragment>

      <TextSegment id="strongHtml">segment2</TextSegment>

    </TextFragment>

  </Page>

</Document>