Convert AI to PNG, JPG, PSD and PDF file with C# .NET

Adobe Illustrator files are majorly used to represent visual information. Moreover, graphic designers transform their creative ideas into masterpieces using AI files. Sometimes you need to convert AI to raster images (PNG, JPG, etc) or PDF file. The most important and exciting factor of all these conversions is that you do not need to install or configure the Adobe Illustrator application because Aspose.PSD for .NET API is capable of converting AI files without any dependency on 3rd party applications. Let us move on to the following use cases in this blog post:

It is important to note here that we will be considering two different AI files as input for a few examples below. One of the input AI file has different paths weight and the other AI file contains complex contours. So let us check out the screenshots for the AI files we will be working with:

AI File with Different Path Weights

AI to PNG

AI File Containing Complex Contours

AI to JPG

Convert AI to PNG with C#

You can enhance your .NET applications to process AI files and convert it to a PNG image. PNG images are quite famous and are often the criteria for submitting images in online forms. So you can follow the below steps to convert AI to PNG files:

  1. Load source AI file
  2. Set properties of PngOptions class
  3. Save AI file as PNG image

Following code snippet is based on the above steps, which shows how to convert AI to PNG with C#:

Convert AI to JPG with C#

Converting AI files to JPG image is simple. You need to follow the steps below:

  1. Load input AI file with AiImage class
  2. Set ImageOptionsBase properties
  3. Save output JPG image

Following code snippet shows how to convert AI to JPG in C# applications:

Convert AI to PSD with C#

PSD files are photoshop documents that contain layered images. You can convert AI to PSD without using Adobe Photoshop or Adobe Illustrator. Follow the steps below in order to perform the conversion:

  1. Load AI input file
  2. Initialize object of PsdOptions class
  3. Save output PSD file as a result

Likewise, the code snippet below shows how to convert AI to PSD in C# .NET applications:

Convert AI to PDF with C#

PDF files format is famous because almost all platforms support .pdf file extension. Moreover, the PDF files are prone to changes and modifications unless permitted by the author of the document. Sometimes you might need to share content with partners or customers without allowing them for any modifications. Therefore, AI to PDF conversion is helpful in such scenarios. You need to follow the below steps to perform the conversion:

  1. Load source AI file
  2. Declare an instance of PdfOptions class
  3. Save output PDF file consequently

Following code snippet shows how to convert AI to PDF with C#:

Conclusion

In short, AI files can be rendered to different file formats with Aspose.PSD for .NET API. For example, we have explored how to convert AI to PNG, AI to JPG, AI to PSD as well as AI to PDF file formats. So you can visit the API documentation and API references to learn more about the API. Moreover, we appreciate your feedback or queries at Free Support Forum.

See Also

Add Fill Layer in PSD file

Posted in Aspose.PSD Product Family | Tagged , , , , , , , | Leave a comment

Draw Graphics and Create 2D Drawings using C# or VB.NET

Draw Graphics using C#

In my previous article, I have given you an overview of our .NET Graphics API – Aspose.Drawing for .NET. Along with that, we have seen how to create images from scratch or draw graphics on existing images using C#. In this article, we’ll go one step ahead and learn how to create 2D drawings by drawing various graphical objects within the .NET applications such as ASP.NET web application, Windows application, and etc.

The following are the vector graphics that we are going to draw within our drawings using .NET Graphics API.

Before we start, make sure you have installed Aspose.Drawing for .NET within your .NET application. You can read about the installation methods in this post.

Create a Drawing from Scratch using C#

First of all, let’s have a look at how to create an empty drawing and save it as a PNG image. The steps are as simple as pie.

You can save the drawing in other image formats as well. The following code sample shows how to create a drawing from scratch using C#.

Draw an Arc in Drawing using C#

Let’s check out how to draw an arc within the drawing. The following are the steps to perform this operation.

The following code sample shows how to draw an arc in the drawing using C#.

Draw an Arc

Draw a Bezier Spline in Drawing using C#

The following are the steps to draw a bezier spline within a drawing.

The following code sample creates a Bezier Spline using C#.

Draw Bezier Spline

Add a Cardinal Spline in Drawing using C#

Use the following steps to draw a cardinal spline using our .NET Graphics API.

The following C# code sample draws a cardinal spline using Aspose.Drawing for .NET.

Draw Cardinal Spline

Add a Closed Curve in Drawing using C#

You can also draw a closed curve in the same way you have drawn the other graphical objects. The following are the steps for this.

You can draw a closed curve using the following C# code sample.

Draw Closed Curve

Draw an Ellipse in a Drawing using C#

The following are the steps to draw an ellipse in a drawing using Aspose.Drawing for .NET.

The following code sample shows how to draw an ellipse using C#.

Draw Ellipse

Draw Lines in a Drawing using C#

Lines are one of the basic objects for creating the drawings. The following are the steps to draw the lines in a drawing using Aspose’ Graphics API.

The following code sample shows how to draw lines within a drawing using C#.

Draw Lines

Draw a Path in the Drawing using C#

The following are the steps to draw a path within a drawing using C#.

The following code sample shows how to draw a path within a drawing using C#.

Draw Path

Draw a Polygon in a Drawing using C#

The following are the steps to draw a polygon within a drawing using Aspose.Drawing for .NET.

This is how you can draw a polygon within a drawing using C#.

Draw Polygon

Draw a Rectangle within the Drawing using C#

Last but not the least, drawing a rectangle. The following are the steps to draw a rectangle.

The following C# code sample draws a rectangle within a drawing.

Draw Rectangle

Conclusion

In this article, we have seen how various graphical objects can be drawn within a drawing programmatically using C#. The step by step guide and code samples demonstrated how to draw lines, rectangles, polygons, arcs, bezier spline, cardinal spline, curves and ellipses within the drawings. You can learn more about our .NET Graphics API using the documentation.

See Also

Posted in Aspose.Drawing Product Family | Tagged , , , | Leave a comment

Load a PDF File to Further Modify and Convert It using C#

In many cases, while working with a PDF document, you need to change it: copy, paste, drag and drop specific PDF content such as text, images, tables, and diagrams. It’s fine to perform these options manually inside the same PDF file as long as you want to work with small parts of your document. But what if you want to perform the editing options in more complex scenarios such as creating a digital signature, merging multiple PDF documents, or reprocessing all the text in a PDF file?

Aspose.Words answers the question with a fast and accurate tool that allows you to load, edit, and convert PDF to any supported file formats such as DOCX, HTML, Markdown, and others. This article will guide you through the usage of these options both programmatically using C# and online.

Features of Loading a PDF Document using C#

Usually, PDFs are used only for viewing, but sometimes you may need to edit or add something new to the existing PDF document.

Aspose.Words provides additional options to work with PDF documents such as:

  • Convert PDF to multiple formats and extract data.
  • Print PDF documents with a variety of printing options.
  • Encrypt and decrypt PDF as well as modify passwords, and others.

In addition to loading a PDF document, you can create PDF documents programmatically in C# and manage the PDF content such as:

  • Add, update, delete text and images.
  • Insert text and image in headers and footers.
  • Manipulate, export, and import annotations.
  • Add, modify, and remove bookmarks.
  • Add tables, graph objects during PDF generation, and others.

Convert from PDF to the Desired Format using C#

One of the main features of Aspose.Words is the ability to easily and reliably convert documents from one file format to another. In Aspose.Words, converting a PDF document format to another format, such as DOCX, is a very simple process and can be accomplished with few lines of code. You can convert PDF to DOCX programmatically in C# by implementing the following conversion steps:

  1. Load a PDF document.
  2. Save the result to the target file format.
Note: You can apply some additional options when loading or saving a document.

Let’s describe the process of converting a PDF document into a DOCX file format in C# by going through the previous steps with all necessary programming details.

Load a PDF Document

Most tasks that you want to perform with Aspose.Words will require you to load a document as a first step of the conversion process. When you load a PDF document into Aspose.Words, it builds its DOM (Document Object Model), and all document elements and formatting are simply loaded into memory. Use one of the document constructors to either create a blank document or load your PDF document. You can create a blank document with any necessary content or load your document from a stream, as it’s explained in the ‘Creating or Loading a Document’ article.

The following example shows how to load a PDF document from a file in C#:

// Load your PDF document into a Document object using one of its constructors
// and the path to your file.
Document doc = new Document(@"C:\DocumentName.pdf");

Save a Document in Any Supported Format

Most tasks that you want to perform with Aspose.Words will require you to save a document as a final step. Aspose.Words supports many different save or export formats that are listed in the SaveFormat enumeration. Use one of the Save methods to save the result to a local file, for example, in the DOCX file format.

To achieve that programmatically in C#, invoke the Document.Save method on the Document object and specify the desired output format as DOCX by entering your file name with the “.docx” extension:

// Save your document as a DOCX file.
doc.Save(dataDir + "DocumentName.docx");

Congratulations, you have successfully managed to convert your PDF file to DOCX.

Apply Additional Options

You can apply different PDF save options or PDF load options, and here are a few examples, such as digital signatures, page range, and document open password. Let’s learn more about them.

Add and Verify Digital Signatures

Aspose.Words allows you to sign a PDF document using the DigitalSignatureDetails PDF save option property. You need to upload a digital certificate from disk or from a certificate store and pass it to a new instance of the X509Certificate2 class.

The following code example shows how to sign a PDF document in C#:

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();

// Create a simple document from scratch.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Load the certificate from disk.
// The other constructor overloads can be used to load certificates from different locations.
X509Certificate2 cert = new X509Certificate2(dataDir + "signature.pfx", "signature");

// Pass the certificate and details to the save options class to sign with.
PdfSaveOptions options = new PdfSaveOptions();
options.DigitalSignatureDetails = new PdfDigitalSignatureDetails();
dataDir = dataDir + "Document.Signed_out.pdf";

// Save the document as PDF.
doc.Save(dataDir, options);

Set Page Range

To set the page range of loading your PDF document, you will need to use the PageIndex property to specify the index of the start page and the PageCount PDF load option property to set the total number of pages to load starting from the specified index:

var loadOptions = new PdfLoadOptions { PageIndex = 2, PageCount = 4 };
var doc = new Document("YourDocumentName.pdf", loadOptions);

Open Protected PDF with Password

You can open an encrypted PDF document by specifying the password value directly in the PDF load options:

var loadOptions = new PdfLoadOptions { Password = "123456" };
var doc = new Document("YourDocumentName.pdf", loadOptions);

PDF Online Converter

If you’re not a programmer, then it’s better to use the online PDF converter. As a first step, all you have to do to convert PDF online is to point your Web browser to the following page on the Aspose Website at the file converter tool.

Let’s guide you through the online conversion process step-by-step:

  1. Drag & drop or select a PDF file to upload by clicking on the button “Drop or Upload your files” as shown in the picture below.
  2. Select DOCX file format from the drop-down list, or choose a different file format.
  3. Click on the button “Convert” to convert your PDF file as shown in the picture below.Convert your PDF
  4. Click on the button “DOWNLOAD NOW” to download the conversion result as a DOCX file. Also, you can view the conversion result on Aspose File Viewer by clicking on the button “VIEW RESULTS”  as shown in the picture below.View results
  5. Congratulations, you have successfully managed to load, convert and save your PDF file to DOCX with our online file converter.

See Also

If you want to achieve the opposite direction and load a Word document to PDF then you can read the following article: Convert Word Document to PDF Programmatically in C# .NET – A Complete Guide.

Posted in Aspose.Words Product Family | Tagged , , | Leave a comment

.NET Graphics API – Create Images and Graphics from Scratch using C#

.NET Graphics API

You would have come across various drawing applications (i.e. MS Paint) that allow you to draw graphics and generate images. However, what if you want to create your own drawing utility or integrate drawing features within your web or desktop application? For such scenarios, Aspose offers its .NET Graphics API as Aspose.Drawing for .NET which is a lightweight solution for creating images and drawings identical to GDI+.

Without any dependencies of the native code and libraries, Aspose.Drawing for .NET lets you draw various graphical objects such as lines, arcs, rectangles, polygons, ellipses, etc. as well as use pens and brushes to create 2D graphics using C# or VB.NET. In this article, I’ll demonstrate how to create an image from scratch or draw graphics on an existing image using C# with our drawing API.

.NET Graphics API – Installation

Before we start, lets first have a look at the installation methods of Aspose.Drawing for .NET. You can either download the API’s DLL or install it using the NuGet within the Visual Studio.

PM> Install-Package Aspose.Drawing

Create an Image from Scratch using C#

In this section, I’ll show you how to use the API for creating an image from scratch. For the demonstration, I’ll add only a couple of graphical objects to the image, however, the same code can be extended for adding the other objects. The following are the steps to create an image using Aspose.Drawing for .NET.

The following code sample shows how to create an image from scratch using C#.

Output

create an image from scratch in C#

You can visit the following documentation article for the code samples of drawing other graphical objects:

Draw Graphics on Existing Images using C#

You may also load an existing image and draw the desired graphical objects on it. Loading and working with an existing image is as simple as creating a new one. The following are the steps to perform this operation.

  • Create an instance of Bitmap class and initialize it with the image’s path.
  • Draw desired graphics such as line, polygon, etc. in the same way you have done in the previous example.
  • Save the updated image using Bitmap.Save(string) method.

The following code sample shows how to draw graphics on an existing image using C#.

Output

draw graphics on image C#

Conclusion

In this article, I have shown you how to use the basic features of Aspose.Drawing for .NET and create images from scratch or add graphics on existing images using C#. In order to explore all the features of the API, you can consult the documentation and download the code samples.

For the upcoming posts and tutorials on Aspose.Drawing for .NET, stay tuned and keep visiting Aspose.Drawing Product Family section.

Posted in Aspose.Drawing Product Family | Tagged , , , | Leave a comment

Add or Remove Watermark in Word Document (DOCX/DOC) in C#

Watermarks are usually used to show ownership or classification of a document. In this article, we will learn to work with watermark in word documents using Aspose.Words for .NET API. Let us cover the following use cases related to watermark.

Add Text Watermark in Word Document with C#

You can add or insert text watermark in a word document, be it a DOC or DOCX file. Simply follow the steps below for adding text watermark:

  1. Load input DOC/DOCX file
  2. Set different TextWatermarkOptions including Font and Layout
  3. Set text of the watermark
  4. Save the output word file

Following code snippet shows how to add watermark in word document (DOC/DOCX) using C#:

The screenshot below is a sample output created by above C# code snippet:

Watermark in word

Add Image Watermark in Word Document with C#

You can also add an image as a watermark in a word document. The image can be added in addition to the text, as we have learned in the previous example. You can follow below steps to add image watermark in a word document:

  1. Load input word file
  2. Initialize ImageWatermarkOptions object
  3. Set image as watermark in the word document
  4. Save the output file

Following code snippet is based on these steps which show how to add image watermark in word documents using C#:

We have used Aspose logo image as sample image so you can observe the output in Print Preview of Microsoft Word application where an image is set as a watermark of the document. Following screenshot depicts the output word file:

image watermark in word file

Remove Watermark in Word Document using C#

You can easily remove or delete a watermark from word document using C#. Let us follow the steps below for removing a watermark from the word document.

  1. Load input word file
  2. Specify the type of watermark to remove
  3. Remove watermark
  4. Save the output word file

Following code snippet shows how to remove a watermark from word document with C#:

Conclusion

We have learned how to add or insert text watermark in word documents. Moreover, we have also explored how to add image watermark in word documents. You can use these features separately or maybe in combination as well, i.e Adding text and image watermark in word document at the same time. Furthermore, your .NET application can be efficient enough to remove or watermarks from word documents. We have created this article as a simple guide to work with watermarks in word. In case of any query or concerns, please contact us via Free Support Forum.

See Also

Create MS Word Editor in ASP.NET MVC

Posted in Aspose.Words Product Family | Tagged , , | Leave a comment

Merge or Unmerge Cells in Excel Worksheet with C#

In this article, we will learn about merging and unmerging of cells in Excel sheet. Generally, merging cells refers to one or more cells joined together to form a large cell. However, the adjacent cells can be merged vertically or horizontally. Whereas, converting a large cell into multiple cells is known as unmerging or splitting cells in Excel worksheet. Let us explore the following use cases:

Merge Cells in Excel Worksheet with C#

You can easily merge cells in excel worksheet with Aspose.Cells for .NET API using C#. In this example, we will be creating a new excel worksheet from scratch and then merge a few cells by following below steps:

  1. Create a Workbook object
  2. Get first worskeet
  3. Merge specific cells
  4. Put value in merged cell
  5. Apply styles on the cell
  6. Save output excel worksheet

Below code snippet shows how to merge cells in Excel worksheet with C#:

The output file generated from this code snippet will contain merged cells as in the screenshot below:

Merge Cells

Unmerge Cells in Excel Worksheet with C#

We have learned how to merge cells in Excel file. Let us take this process another step further. Here we will be considering to unmerge cells in Excel worksheet by following the steps below:

  1. Load source Excel file
  2. Create Worksheet object and access first sheet
  3. Unmerge the cells
  4. Save output file

The code snippet below is based on these steps and shows how to unmerge cells in Excel file with C#:

The following image shows how do the unmerged cells look when you run the above code in your environment:

Unmerge Cells

Merge a Range of Cells in Excel Worksheet with C#

Sometimes there are several cells specified under different ranges of cells. You might want to merge a specific range of cells in Excel sheet. In such scenarios, please follow the steps listed below:

  1. Create a Workbook object
  2. Access the first worksheet
  3. Input data in a Cell
  4. Create and Merge the Range
  5. Save output excel file

The code snippet below shows how to merge range of cells into one larger cell in Excel file using C#:

Unmerge a Range of Cells in Excel Worksheet with C#

We have explored merging a range of cells in Excel in the previous example. Let us move on to unmerging the merged cells by following the steps below:

  1. Load source Workbook
  2. Access specific Worksheet
  3. Create a Range object
  4. Call UnMerge method
  5. Save output Excel file

The code snippet below follows these steps and shows how to unmerge a range of cells in Excel worksheet using C#:

Merge Cells in Named Range in Excel Worksheet with C#

Sometimes the Excel files contain several named ranges because it is easy to identify and manipulate accordingly. Therefore, you can also merge any named range by specifying its name. Resultantly, all the cells in that named range will be merged into one bigger cell. You need to follow these steps:

  1. Load source Excel Workbook
  2. Access the Worksheet containing the named range
  3. Define and Apply style
  4. Merge Named Range
  5. Save output file

The code snippet below explains how to merge cells in a named range in C#:

The output of the above code snippet will be the same as the following image:

Merge Named Range

Conclusion

Merging or unmerging cells in Excel Workbooks and Worksheets is an important and useful feature. We have compiled different possible use cases in this article along with all the steps and images. Now you can easily follow these steps and merge or unmerge cells, ranges, or named ranges in Excel files. In case of any query or concern, you may please contact us via Free Support Forum.

See Also

Import Data from JSON to Excel Worksheet in C# .NET

Posted in Aspose.Cells Product Family | Tagged , , , , , , | Leave a comment

Convert PDF Documents to Excel XLS/XLSX using Java

PDF to Excel Java

There could be various scenarios when you need to export the data within the tables from PDF to Excel workbooks. PDF to Excel lets you edit the data, apply formulas, plot charts, and perform other operations that are supported by the spreadsheets. In this article, I’ll show you how to automate the PDF to Excel conversion and convert PDF to XLS to XLSX workbooks programmatically using Java. For this, we’ll leverage the capabilities of Aspsoe.PDF for Java API.

Java PDF to Excel Converter – Installation

Aspose.PDF for Java is available for download as a JAR file. In addition, you can install it in your Maven-based applications using the following configurations.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>20.5</version>
    <classifier>jdk17</classifier>
</dependency>

Convert PDF to Excel XLS using Java

In order to convert PDF to Excel XLS, you simply need to load the PDF document and save it with .xls extension. The following are the steps to perform this conversion.

The following code sample shows how to convert PDF to Excel XLS using Java.

Input PDF Document

Convert PDF to Excel

Output Excel Spreadsheet

PDF to XLS XLSX

Convert PDF to Excel XLSX using Java

For converting PDF to XLSX spreadsheet, you need to define the output Excel format using ExcelSaveOptions class. The following are the steps to perform PDF to XLSX conversion using Aspose.PDF for Java.

The following code sample shows how to convert PDF to XLSX using Java.

Minimize the Number of Worksheets in PDF to Excel

By default, Aspose.PDF converts each page in the PDF document to a separate worksheet. However, you may customize the API to minimize the number of worksheets in case of large PDF documents. The following are the steps to perform this operation.

The following code sample shows how to convert PDF to XLSX with a minimized number of worksheets using Java.

Conclusion

In this article, you have seen how to convert PDF to Excel XLS or XLSX files using Java. Furthermore, the feature of minimizing the number of worksheets in PDF to Excel is also discussed with the help of the code sample. You can explore more about Aspose.PDF for Java using the documentation.

See Also

Posted in Aspose.PDF Product Family | Tagged , , , | Leave a comment

Convert Image to Text using C++ OCR Library

Perform OCR using C++

The OCR (Optical Character Recognition) technology lets you read and convert the text in the images or scanned documents to the machine-readable form. The OCR has various use cases that include reading codes from vouchers, making the text editable, self-service stores, converting printed documents into digital formats, and so on. Various OCR tools and libraries are available in the market, however, the reliability of the OCR results is a key factor. In this article, I’ll show you how to create your own OCR application and convert images to text programmatically using C++.

C++ OCR Library to Convert Image to Text

Aspose offers a powerful C++ OCR library that uses deep learning to provide you more reliable and near to accurate OCR results. You can download the library files as well as a sample project from GitHub.

Convert Image Page to Text using C++

Lets first check out the scenario in which the image contains a multi-line text. This could be the case when you have a scanned book where each page of the book contains a bunch of text lines. The following are the steps to convert image to text in this case.

The following code sample shows how to perform OCR and convert image to text using C++.

Perform OCR for an Image with Single Line using C++

In the previous example, we have converted an image having multiple text lines. However, there could be the case when the image contains only a single line of text such as the caption or a slogan. The following are the steps to perform OCR in such cases.

The following code sample shows how to perform OCR on an image with a single line of text using C++.

Convert a Particular Area of Image to Text in C++

You can also customize the API to limit the area of the image where you want to perform the OCR. In this case, you can create a rectangle on the image to access the desired area. The following are the steps to extract text from a particular area of the image.

The following code sample shows how to convert a particular area of the image to text using C++.

Conclusion

In this article, you have learned how to use Aspose’ OCR library to convert images to text in C++. We have seen how to perform OCR on an image with single or multiple lines of text as well as read text from a particular area of an image. You can learn more about Aspose.OCR for C++ using the documentation.

See Also

Posted in Aspose.OCR Product Family | Tagged , | Leave a comment

Convert XML to PDF – Easy XML to PDF Conversion

XML to PDF

In this article, we will be converting XML files to PDF using C#. Aspose.PDF for .NET API extensively supports the XML conversion feature because of the inclusions of many related enhancements, as requested by the API users. You can elevate your .NET applications with this efficient feature of exporting XML to PDF. Let us consider following use cases related to XML conversion using C#:

Convert XML to PDF

For converting an XML to PDF, you need to follow the XML Schema of Aspose.PDF for .NET API which is available as XSD file. Following is an XML file that we will be converting to PDF as a Hello World demonstration.

<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
  <Page id="mainPage">
    <TextFragment>
      <TextSegment>Hello</TextSegment>
    </TextFragment>
 
    <TextFragment>
      <TextSegment>World!</TextSegment>
    </TextFragment>
  </Page>
</Document>

You need to follow the steps below to convert XML to PDF file:

  1. Initialize an object of Document Class
  2. Load XML file with BindXml method
  3. Save the converted PDF file

Following code snippet shows how to convert XML to PDF in C#:

Use HTML in XML and Convert to PDF

Sometimes you might need to use HTML in XML prior conversion to HTML. Aspose.PDF for .NET API supports this feature as well. However, HTML and XML tags are quite similar. Therefore, you need to specify the CDATA tag so that the HTML is not parsed as XML markup. Below sample XML file includes HTML denoted by CDATA to avoid any anomaly:

<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
  <Page id="mainSection">
    <HtmlFragment>
      <![CDATA[
        <font style="font-family:Tahoma; font-size:40px;">This is Html String.</font>
        ]]>
    </HtmlFragment>
  </Page>
</Document>

You can convert this XML file to PDF with following steps:

  1. Instantiate Document class object
  2. Load the input XML file
  3. Save the output PDF file

Below code snippet shows how to convert an XML file, containing HTML, to PDF in C#:

Convert XML and XSLT to PDF

Sometimes you may have an existing XML file that contains important application data and you want to generate PDF reports using that XML file. In such scenarios, you can create XSLT file to transform your existing XML document to Aspose.PDF’s compatible XML document. Then you can proceed to convert XML to PDF. Let us learn this with simple and basic example:

<?xml version="1.0" encoding="utf-8" ?>
<Contents>
  <Content>Hello World!</Content>
</Contents>
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text()"/>
    <xsl:template match="/Contents">
    <html>
      <Document xmlns="Aspose.Pdf" IsAutoHyphenated="false">
        <PageInfo>
          <DefaultTextState
                            Font = "Helvetica" FontSize="8" LineSpacing="4"/>
          <Margin Left="5cm" Right="5cm" Top="3cm" Bottom="15cm" />
        </PageInfo>
        <Page id="mainSection">
          <TextFragment>
            <TextSegment>
              <xsl:value-of select="Content"/>
            </TextSegment>
          </TextFragment>
        </Page>
      </Document>
    </html>
</xsl:template>
</xsl:stylesheet>

You can notice that the XML file does not follow the XML schema of Aspose.PDF for .NET API. However, the XSLT file transforms it to required compatibility. Now you can follow the steps below to convert such XML using XSLT to PDF:

  1. Initialize PDF Document
  2. Bind XML and XSLT file
  3. Save the output PDF document

The code snippet below is based on these steps which show how to convert XML to PDF in C#:

Conclusion

We have learned how to convert XML to PDF file provided the XML follows the mentioned schema of the API. Considering the popularity and usability of HTML tags, we have also considered the scenario when you need to include some HTML in the source XML file. Moreover, we have also explored how to convert XML and XSLT to PDF when the existing XML file does not follow the schema.

See Also

Merge Multiple PDF Files into One PDF using C# .NET

Posted in Aspose.PDF Product Family | Tagged , , , , | Leave a comment

Merge Multiple PDF Files into a Single PDF using Java

Merge PDF Files into Single PDF

In various scenarios, you come across the need of merging two or more PDF documents into a single file. For example, PDF merging allows you to combine similar kinds of documents. Furthermore, you can merge multiple PDFs into a single PDF before sharing it online or sending it to someone. In this article, I’ll demonstrate how to automate this feature and merge multiple PDF files programmatically using Java.

Java API to Merge PDF Files

Aspose.PDF for Java is a feature-rich PDF API that lets you merge multiple PDF documents quite easily within a few lines of code. You can either download the API’s JAR or install it in your Maven-based application using the following configuration.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>20.5</version>
    <classifier>jdk17</classifier>
</dependency>'

Merge Two PDF Files into One PDF in Java

Lets first check out the simple scenario of merging only two PDF files and this can be achieved within a couple of steps.

The following code sample shows how to merge two PDF files into a single PDF using Java.

Merge Multiple PDF Files using Java

In the previous example, we have merged only two PDF files into a single PDF. However, there could be a case when you need to merge more than two PDF files. In such cases, you can pass an array to the PDF files’ paths to the concatenate method. The following are the steps to perform this operation.

The following code sample shows how to merge multiple PDF files into a single PDF using Java.

Merge PDF Files using InputStream in Java

In case you are dealing with the PDF files in the form of InputStream, you can directly pass the InputStream objects and get the merged PDF as an OutputStream object. The following are the steps to merge PDF files loaded into InputStream objects.

The following code sample shows how to merge PDF files using InputStream objects in Java.

Conclusion

In this article, you have learned how to merge PDF files programmatically using Java. The code samples and step by step guide shows how to merge two or more than two PDF files using physical paths or InputStream objects. You can learn more about the Java PDF Merger library using the documentation.

See Also

Posted in Aspose.PDF Product Family | Tagged , | Leave a comment