One of the great things about being a programmer is that when you need software that you don’t have, you can usually write a small utility application to do what you need without having to purchase the software.
If you own Adobe Acrobat ($299 USD) or FoxIt Editor ($99.00 USD), then you can just right click and extract pages from an existing PDF to create a new PDF document. However, if you don’t want to shell out the money for it, then you can always write your own code to perform the same task.
I used iTextSharp in a previous application to provide a capability of exporting documents to PDF, so I was familiar with the iTextSharp project. In that application, I was able to create PDF documents for various paper sizes ranging from 8 1/2 x 11 and legal sizes to large format plotter pages sizes such as D and E-size pages. The library worked great and produce near perfect PDF documents of our displays.
Today, I needed to extract a couple pages from a PDF file to create a new PDF document and I realized that I didn’t have a PDF editor… So I went out and downloaded iTextSharp library and wrote a small program to do the work for me.
I’ll just brush over the basic program setup.
I used a C# console application as my starting point
There are 4 command line arguments (input file, output file, starting page, and ending page)
Validate that the input file exists
Validate that the input file is a PDF document
Validate that the starting and ending page numbers are valid
Add two using directives for iTextSharp.text and iTextSharp.text.pdf
Okay, here’s the primary method of the application. You can see the four input parameters and the code comments should provide enough information to walk you through the steps.private static void ExtractPages(string inputFile, string outputFile,
int start, int end)
{
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end <> pageCount)
{
end = pageCount;
}
// load the input document
Document inputDoc =
new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// copy pages from input to output document
for (int i = start; i <= end; i++)
{
inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(i));
inputDoc.NewPage();
PdfImportedPage page =
outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
inputPdf.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
inputDoc.Close();
}
}
Thursday, September 16, 2010
How to merge pages from a PDF document
This article will use the same library, iTextSharp, to merge pages from one PDF document to create a second PDF document.
For this utility, imagine having a PDF document with pages that are 8 1/2″ x 11″ and you want to combine 2 pages into one larger page. The resulting output document would be 17″ x 11″ and show two pages from the input document on one page on the output document.
I’ll just brush over the basic program setup.
I used a C# console application as my starting point
There are 2 command line arguments (input file and output file)
Validate that the input file exists
Validate that the input file is a PDF document
Add two using directives for iTextSharp.text and iTextSharp.text.pdf
Here’s the primary method of the application. The code comments should provide enough information to walk you through the steps.private static void MergePages(string inputFile, string outputFile)
{
// get input document
PdfReader inputPdfReader = new PdfReader(inputFile);
// load the input document
Document inputPdfDoc =
new Document(inputPdfReader.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter =
PdfWriter.GetInstance(inputPdfDoc, fs);
inputPdfDoc.Open();
PdfContentByte pdfCb = outputWriter.DirectContent;
// loop through each page in input pdf reader
for (int i = 1; i <= inputPdfReader.NumberOfPages; i++) { float leftOffset = 1f; // if even page (0,2,4,...) if (i % 2 == 1) { Rectangle inputPageSize = inputPdfReader.GetPageSizeWithRotation(i); Rectangle outputPageSize = new Rectangle(0, 0, inputPageSize.Width * 2, inputPageSize.Height); //add new page that is x2 width inputPdfDoc.SetPageSize(outputPageSize); inputPdfDoc.NewPage(); // draw vertical line pdfCb.MoveTo(inputPageSize.Width, 0); pdfCb.LineTo(inputPageSize.Width, inputPageSize.Height); pdfCb.Stroke(); } else { leftOffset = inputPdfReader.GetPageSizeWithRotation(i).Width; } // get page[i] from input pdf PdfImportedPage inputPage = outputWriter.GetImportedPage(inputPdfReader, i); // add page[i] to new pdf on current page with offset pdfCb.AddTemplate(inputPage, leftOffset, 0); } inputPdfDoc.Close(); } }
For this utility, imagine having a PDF document with pages that are 8 1/2″ x 11″ and you want to combine 2 pages into one larger page. The resulting output document would be 17″ x 11″ and show two pages from the input document on one page on the output document.
I’ll just brush over the basic program setup.
I used a C# console application as my starting point
There are 2 command line arguments (input file and output file)
Validate that the input file exists
Validate that the input file is a PDF document
Add two using directives for iTextSharp.text and iTextSharp.text.pdf
Here’s the primary method of the application. The code comments should provide enough information to walk you through the steps.private static void MergePages(string inputFile, string outputFile)
{
// get input document
PdfReader inputPdfReader = new PdfReader(inputFile);
// load the input document
Document inputPdfDoc =
new Document(inputPdfReader.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter =
PdfWriter.GetInstance(inputPdfDoc, fs);
inputPdfDoc.Open();
PdfContentByte pdfCb = outputWriter.DirectContent;
// loop through each page in input pdf reader
for (int i = 1; i <= inputPdfReader.NumberOfPages; i++) { float leftOffset = 1f; // if even page (0,2,4,...) if (i % 2 == 1) { Rectangle inputPageSize = inputPdfReader.GetPageSizeWithRotation(i); Rectangle outputPageSize = new Rectangle(0, 0, inputPageSize.Width * 2, inputPageSize.Height); //add new page that is x2 width inputPdfDoc.SetPageSize(outputPageSize); inputPdfDoc.NewPage(); // draw vertical line pdfCb.MoveTo(inputPageSize.Width, 0); pdfCb.LineTo(inputPageSize.Width, inputPageSize.Height); pdfCb.Stroke(); } else { leftOffset = inputPdfReader.GetPageSizeWithRotation(i).Width; } // get page[i] from input pdf PdfImportedPage inputPage = outputWriter.GetImportedPage(inputPdfReader, i); // add page[i] to new pdf on current page with offset pdfCb.AddTemplate(inputPage, leftOffset, 0); } inputPdfDoc.Close(); } }
Merge PDF Files using iTextSharp
iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.The code of the class I've written uses iText# and is based on the example code (Console Application) that can be found on http://itextsharp.sourceforge.net/examples/Concat.cs . However, this code seems to target an out of date version of iText# and can't be compiled without fixing some lines...This C#-class will allow you to merge multiple PDF's to one big PDF-file:// Based on : http://itextsharp.sourceforge.net/examples/Concat.csusing System;using System.IO;using iTextSharp.text;
using iTextSharp.text.pdf;
public class PdfMerge{public static void MergeFiles(string destinationFile, string[] sourceFiles){
try
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;int rotation;
// step 4: we add content
while (f < sourceFiles.Length)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
Console.WriteLine("Processed page " + i);
}
f++;
if (f < sourceFiles.Length)
{
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
}
}
// step 5: we close the documentdocument.Close();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}
}
using iTextSharp.text.pdf;
public class PdfMerge{public static void MergeFiles(string destinationFile, string[] sourceFiles){
try
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;int rotation;
// step 4: we add content
while (f < sourceFiles.Length)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
Console.WriteLine("Processed page " + i);
}
f++;
if (f < sourceFiles.Length)
{
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
}
}
// step 5: we close the documentdocument.Close();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}
}
Tuesday, September 7, 2010
Split function in SQL server 2005
ALTER function [dbo].[fn_split](
@str varchar(8000),
@delimiter char(1)
)
returns @returnTable table (idx int primary key identity, item varchar(8000))
as
begin
declare @pos int
select @str = @str + @delimiter
while len(@str) > 0
begin
select @pos = charindex(@delimiter,@str)
if @pos = 1
insert @returnTable (item)
values (null)
else
insert @returnTable (item)
values (substring(@str, 1, @pos-1))
select @str = substring(@str, @pos+1, len(@str)-@pos)
end
return
end
Calling:
select convert(int,item) from fn_split(@stringvalue)
example:
INSERT INTO member (intid,phoneid)
select convert(int,item) ,1
from fn_split(@vchmemberid,',')
@str varchar(8000),
@delimiter char(1)
)
returns @returnTable table (idx int primary key identity, item varchar(8000))
as
begin
declare @pos int
select @str = @str + @delimiter
while len(@str) > 0
begin
select @pos = charindex(@delimiter,@str)
if @pos = 1
insert @returnTable (item)
values (null)
else
insert @returnTable (item)
values (substring(@str, 1, @pos-1))
select @str = substring(@str, @pos+1, len(@str)-@pos)
end
return
end
Calling:
select convert(int,item) from fn_split(@stringvalue)
example:
INSERT INTO member (intid,phoneid)
select convert(int,item) ,1
from fn_split(@vchmemberid,',')
Comma Separated value in SQL
create function [dbo].[commaSeperateValue]
(
@inttypeid int,
@intmemberid int,
@parametertypeid int
)
returns varchar(1000)
as
begin
declare @Values varchar(1000)
if @inttypeid = 1 -- buiding types
select @Values = Stuff((SELECT DISTINCT ', ' + vchBuildingType AS [text()]
FROM member_building_type mbt inner join building_type bt on bt.intbuildingtypeid=mbt.intbuildingtypeid
and bt.bitIsActive=1 and mbt.intmemberid = @intmemberid and mbt.intBuildingTypeId = (case when @parametertypeid = 0 then mbt.intBuildingTypeId else @parametertypeid end)
FOR XML PATH ('')),1,1,'')
if @inttypeid = 2 -- Role types
select @Values = Stuff((SELECT DISTINCT ', ' + vchRoleName AS [text()]
FROM email_template_roles mbt inner join [role] bt on bt.introleId=mbt.introleId
and bt.bitActive=1
where mbt.intTemplateId=@intmemberid
FOR XML PATH ('')),1,1,'')
if @inttypeid = 3 -- Services
select @Values = Stuff((SELECT DISTINCT ', ' + vchServiceType AS [text()]
FROM member_setting_zipcode msz
inner join service s on s.intServiceId=msz.intserviceid
inner join service_type st on st.intServiceTypeId=s.intServiceId
and msz.intserviceid = (case when @parametertypeid = 0 then msz.intserviceid else @parametertypeid end)
and msz.intmemberid=@intmemberid
FOR XML PATH ('')),1,1,'')
return @Values
end
Calling :
select dbo.lookup_types_commaSeperate (2,4,0)
Output:
Admin, Chapter President,Inspector,Contractor
(
@inttypeid int,
@intmemberid int,
@parametertypeid int
)
returns varchar(1000)
as
begin
declare @Values varchar(1000)
if @inttypeid = 1 -- buiding types
select @Values = Stuff((SELECT DISTINCT ', ' + vchBuildingType AS [text()]
FROM member_building_type mbt inner join building_type bt on bt.intbuildingtypeid=mbt.intbuildingtypeid
and bt.bitIsActive=1 and mbt.intmemberid = @intmemberid and mbt.intBuildingTypeId = (case when @parametertypeid = 0 then mbt.intBuildingTypeId else @parametertypeid end)
FOR XML PATH ('')),1,1,'')
if @inttypeid = 2 -- Role types
select @Values = Stuff((SELECT DISTINCT ', ' + vchRoleName AS [text()]
FROM email_template_roles mbt inner join [role] bt on bt.introleId=mbt.introleId
and bt.bitActive=1
where mbt.intTemplateId=@intmemberid
FOR XML PATH ('')),1,1,'')
if @inttypeid = 3 -- Services
select @Values = Stuff((SELECT DISTINCT ', ' + vchServiceType AS [text()]
FROM member_setting_zipcode msz
inner join service s on s.intServiceId=msz.intserviceid
inner join service_type st on st.intServiceTypeId=s.intServiceId
and msz.intserviceid = (case when @parametertypeid = 0 then msz.intserviceid else @parametertypeid end)
and msz.intmemberid=@intmemberid
FOR XML PATH ('')),1,1,'')
return @Values
end
Calling :
select dbo.lookup_types_commaSeperate (2,4,0)
Output:
Admin, Chapter President,Inspector,Contractor
Get Address image from Google Map and Add it into iTextSharp Document PDF
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?center=" + objInspMgt.PropertyInspectedAddress + "&zoom=14&size=612x612&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|41.726247,-97.002853&sensor=false");
//Map style roadMap
iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=roadmap&markers=size:mid|color:red|"+objInspMgt.PropertyInspectedAddress+"&sensor=false");
//Map style terrain
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=terrain&markers=size:mid|color:red|" + objInspMgt.PropertyInspectedAddress + "&sensor=false");
//Map style satellite
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=satellite&markers=size:mid|color:red|"+objInspMgt.PropertyInspectedAddress+"&sensor=false");
//Map style hybrid
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=hybrid&markers=size:mid|color:red|" + objInspMgt.PropertyInspectedAddress + "&sensor=false");
LocationImage.ScaleToFit(650, 650);
LocationImage.Alignment = iTextSharp.text.Image.UNDERLYING;
LocationImage.SetAbsolutePosition(0, 0);
locationcell = new PdfPCell(LocationImage);
locationcell.PaddingBottom = 10;
locationcell.PaddingTop = 10;
locationcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
locationcell.Colspan = 3;
locationcell.Border = 0;
Locationtable.AddCell(locationcell);
Document.Add(Locationtable);
//Map style roadMap
iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=roadmap&markers=size:mid|color:red|"+objInspMgt.PropertyInspectedAddress+"&sensor=false");
//Map style terrain
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=terrain&markers=size:mid|color:red|" + objInspMgt.PropertyInspectedAddress + "&sensor=false");
//Map style satellite
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=satellite&markers=size:mid|color:red|"+objInspMgt.PropertyInspectedAddress+"&sensor=false");
//Map style hybrid
//iTextSharp.text.Image LocationImage = iTextSharp.text.Image.GetInstance("http://maps.google.com/maps/api/staticmap?size=412x530&maptype=hybrid&markers=size:mid|color:red|" + objInspMgt.PropertyInspectedAddress + "&sensor=false");
LocationImage.ScaleToFit(650, 650);
LocationImage.Alignment = iTextSharp.text.Image.UNDERLYING;
LocationImage.SetAbsolutePosition(0, 0);
locationcell = new PdfPCell(LocationImage);
locationcell.PaddingBottom = 10;
locationcell.PaddingTop = 10;
locationcell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
locationcell.Colspan = 3;
locationcell.Border = 0;
Locationtable.AddCell(locationcell);
Document.Add(Locationtable);
Subscribe to:
Posts (Atom)