Showing posts with label itextsharp. Show all posts
Showing posts with label itextsharp. Show all posts

Sunday, March 14, 2010

Add image to pdf


using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

public class AddImage{

public static void Main() {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileStream("Example.pdf", FileMode.Create));
document.Open();

Image wmf = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/harbour.wmf"));
Image gif = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/vonnegut.gif"));
Image jpeg = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/myKids.jpg"));
Image png = Image.getInstance(new Uri("http://itextsharp.sourceforge.net/examples/hitchcock.png"));

document.Add(wmf);
document.Add(gif);
document.Add(jpeg);
document.Add(png);
}

catch(DocumentException de) {
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe) {
Console.Error.WriteLine(ioe.Message);
}
document.Close();
}
}

C# Concat two pdf

int f = 1;
PdfReader reader = new PdfReader("Chap0101.pdf");
int n = reader.NumberOfPages;
Document documentUnit = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(documentUnit, new FileStream("Chap0103.pdf", FileMode.Create));
documentUnit.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;

while (f < 3)
{
  int i = 0;
while (i < n)
{
i++;
documentUnit.SetPageSize(reader.GetPageSizeWithRotation(i));
documentUnit.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);
}
}
f++;
if (f < 3)
{
reader = new PdfReader("Chap0102.pdf");
n = reader.NumberOfPages;
}
}

documentUnit.Close();

C# Create pdf documents with itextsharp

iText# (iTextSharp) an open source C# library for PDF generation.
To create a new pdf you must download itextsharp.dll from itextsharp.sourceforge.com and add it to the project.



using iTextSharp.text.pdf;
using iTextSharp.text;
......
static Font fontHeaderBold = new Font(1, 10, 1);
static Font fontHeader = new Font(1, 10, Font.NORMAL);
static Font fontHeaderTabelBold = new Font(1, 9, Font.BOLD);
static Font fontHeaderTabel = new Font(1, 6, Font.NORMAL);
static Font fontBlank = new Font(1, 6, Font.NORMAL, new BaseColor(255, 255, 255));
............
Document document = new Document(new Rectangle(0, 0, 942, 595));
document.SetMargins(0, 0, 30, 30);
PdfWriter pdfwriter = PdfWriter.GetInstance(document, new FileStream("Doc.pdf", FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(7);
float[] widths = new float[] { 105f, 70f, 25f, 10f, 40f, 10f, 100f };
table.SetWidths(widths);

PdfPCell cell0 = new PdfPCell(new Phrase("text1"));
cell0.Border = 0;
PdfPCell cell1 = new PdfPCell(new Phrase("text1", fontHeaderBold));
cell1.Border = 0;
PdfPCell cell2 = new PdfPCell(new Phrase("text1", fontHeaderBold));
cell2.Border = 0;
PdfPCell cell3 = new PdfPCell(new Phrase(" text1 "));
cell3.BorderWidth = 1;
PdfPCell cell4 = new PdfPCell(new Phrase("text1", fontHeaderBold));
cell4.Border = 0;

PdfPCell cell5 = new PdfPCell(new Phrase("text1 "));
cell5.BorderWidth = 1;
PdfPCell cell6 = new PdfPCell(new Phrase("text1"));
cell6.BorderWidth = 0;
PdfPCell cell7 = new PdfPCell(new Phrase("text1", fontHeaderBold));
cell7.BorderWidth = 0;
PdfPCell cell8 = new PdfPCell(new Phrase("text1", new Font(1, 11, 1)));
cell8.Border = 0;
cell8.Colspan = 6;

table.AddCell(cell0);
table.AddCell(cell1);
table.AddCell(cell2);
table.AddCell(cell3);
table.AddCell(cell4);
table.AddCell(cell5);
table.AddCell(cell6);
table.AddCell(cell7);
table.AddCell(cell8);

Paragraph p = new Paragraph("text1", fontHeaderBold);
p.Alignment = 1;
Paragraph p2 = new Paragraph("text1 ", fontHeaderBold);
p2.Alignment = Element.ALIGN_RIGHT;
Paragraph p3 = new Paragraph("text1", fontHeaderBold);
p3.Alignment = Element.ALIGN_RIGHT;

document.Add(table);
document.Add(p);
document.Add(p2);
document.Add(p3);
document.Close();