Recently I was in need of writing to existing Pdf files. I
thought of using itextsharp library in order to do the Pdf files manipulations.
A useful file can be downloaded from http://www.manning.com/lowagie2/samplechapter6.pdf
PdfReader - Reading the existing Pdf document
PdfStamper - Starts the process of adding extra content to an existing PDF document keeping the
document PDF/A conformant.
PdfContentByte - PdfContentByte is an object containing the user positioned text and graphic
contents of a page. It knows how to apply the proper font encoding.
Below is Example Code:
private void WriteToPdfFile(string sourceFile, string destinatationFile)
{
// Reading the source file template supplied
PdfReader reader = new PdfReader(sourceFile);
//Create the destication file supplied
PdfStamper stamper = new PdfStamper(reader, new
FileStream(destinatationFile, FileMode.Create));
// Get the Page1 and Page2 Content - Graphical state of the page
PdfContentByte page1 = stamper.GetOverContent(1);
PdfContentByte page2 = stamper.GetOverContent(2);
// Declare the Font Size
Font courier10 = new Font(Font.FontFamily.COURIER, 10f);
Font Cambria12 = FontFactory.GetFont("Cambria", 12f);
int xPosition = 30;
int yPosition = 720;
// Writing the content to specific position
ColumnText.ShowTextAligned(page1, Element.ALIGN_LEFT,
new Phrase("Hello iTextSharp", courier10), xPosition, yPosition, 0);
// setting the file compression to file size is reduced
stamper.SetFullCompression();
stamper.Close();
reader.Close();
}
// Please return to me in case you find any problem.
No comments:
Post a Comment