How To Rasterize A PDF Document In .NET
Learn to convert text, shapes, and images in a PDF page as a single graphic item using PDFOne .NET.
By V. Subhash
A page in a PDF document can contain any combination of text, images,
shapes, and other page elements. Sometimes, it becomes necessary to
convert all these page elements as a single image element, say, to prevent
text from being copied word by word.
PDFOne .NET
ProPlus edition can export PDF pages as images. It
can also draw images on PDF pages. Combining these two capabilities, you
can convert your existing PDF documents with heterogeneous content to PDF
documents with just images. Such documents are called rasterized documents.
Here is the code to do it.
Imports Gnostice.PDFOne
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
'Create two PDFDocument instances for input and output documents
Dim PDFDocument1 As New PDFDocument("your licence key")
Dim PDFDocument2 As New PDFDocument("your licence key")
Dim MetaFile1 As Metafile
Dim PDFPage1, PDFPage2 As PDFPage
Dim iCounter, iPageCount As Integer
Try
' Load the input document and get its page count
PDFDocument1.Load("input_doc.pdf")
iPageCount = PDFDocument1.GetPageCount
' Iterate through all pages in the input document, export
' each page as a vector image, convert each vector image
' to a raster image, and add raster image to the output
' document.
For iCounter = 1 To iPageCount
' Export current page as vector image
MetaFile1 = PDFDocument1.GetPageMetafile(iCounter)
' Save meta file as a raster image
MetaFile1.Save("image_of_page_#" & iCounter & ".jpg", _
ImageFormat.Jpeg)
' Release resources used by the vector image
MetaFile1.Dispose()
' Access current page
PDFPage1 = PDFDocument1.GetPage(iCounter)
' Create a new page with current page dimensions
PDFPage2 = New PDFPage( _
PDFPage1.GetWidth(PDFMeasurementUnit.Inches), _
PDFPage1.GetHeight(PDFMeasurementUnit.Inches))
' Draw raster image on the new page
PDFPage2.DrawImage("image_of_page_#" & iCounter & ".jpg", 0, 0)
' Add the new page to the output document
PDFDocument2.AddPage(PDFPage2)
Next
' Save rasterized output document
PDFDocument2.Save("rasterized_output_doc.pdf")
Catch ex As Exception
MsgBox(ex.Message)
Finally
PDFDocument1.Close()
PDFDocument1.Dispose()
PDFDocument2.Close()
PDFDocument2.Dispose()
Me.Close()
End Try
End Sub
End Class
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Gnostice.PDFOne;
namespace PN_Rasterize_PDF2 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
// Create two PDFDocument instances for input and output documents
PDFDocument PDFDocument1
= new PDFDocument("your licence key");
PDFDocument PDFDocument2
= new PDFDocument("your licence key");
PDFPage PDFPage1, PDFPage2;
Metafile Metafile1;
int iCounter, iPageCount;
try {
// Load the input document and get its page count
PDFDocument1.Load("input_doc.pdf");
iPageCount = PDFDocument1.GetPageCount();
// Iterate through all pages in the input document, export
// each page as a vector image, convert each vector image
// to a raster image, and add raster image to the output
// document.
for (iCounter = 1; iCounter < iPageCount; iCounter++) {
// Export current page as vector image
Metafile1 = PDFDocument1.GetPageMetafile(iCounter);
// Save meta file as a raster image
Metafile1.Save(
"image_of_page_#" + iCounter.ToString() + ".tiff",
ImageFormat.Tiff);
// Release resources used by the vector image
Metafile1.Dispose();
// Access current page
PDFPage1 = PDFDocument1.GetPage(iCounter);
// Create a new page with current page dimensions
PDFPage2
= new PDFPage(PDFPage1.GetWidth(PDFMeasurementUnit.Inches),
PDFPage1.GetHeight(PDFMeasurementUnit.Inches));
// Draw raster image on the new page
PDFPage2.DrawImage(
"image_of_page_#" + iCounter.ToString() + ".tiff", 0, 0);
// Add the new page to the output document
PDFDocument2.AddPage(PDFPage2);
}
// Save rasterized output document
PDFDocument2.Save("rasterized_output_doc.pdf");
}
catch (Exception Err) {
MessageBox.Show(Err.Message);
}
finally {
PDFDocument1.Close();
PDFDocument1.Dispose();
PDFDocument2.Close();
PDFDocument2.Dispose();
this.Close();
}
}
}
}
|
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels.
Windows users are advised to use Microsoft ClearType Tuning for optimal experience.
Linux and other users can enable font smoothing, as supported by their OS.
Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.