iTextSharp : create one more page copying last page - c#

I have a PDF file with headers, footers etc on every page. I need to extend count of pages, if text is very long (I have variable text). For example:
if (countOfRecords > 120)
{
// add one more page like second page of template (which does not have content, only header/footer
}
Is it possible?

I implemented it the following way: created PDF file with "empty" page (means only with necessary headers/footers) and then add it to main PDF if necessary how many times as necessary:
PdfReader pdfReader = new PdfReader(model.Input);
Document document = new Document(pdfReader.GetPageSizeWithRotation(1));
using (MemoryStream ms = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page = writer.GetImportedPage(pdfReader, 1);
cb.AddTemplate(page, 0, 0);
int countOfPages = (int)Math.Ceiling(Convert.ToDecimal(model.ActiveDriverList.Count - countDriversOnFirstPage) / countDriversOnEmptyPage);
for (int i = 0; i < countOfPages; i++)
{
PdfReader readerPage = new PdfReader(model.EmptyPage);
readerPage.ConsolidateNamedDestinations();
document.SetPageSize(pdfReader.GetPageSizeWithRotation(1));
document.NewPage();
PdfImportedPage importedPage = writer.GetImportedPage(readerPage, 1);
cb = AddTextDriversNextPage(cb, model.ActiveDriverList, i + 1);
cb.AddTemplate(importedPage, 0, 0);
}
document.Close();
writer.Close();
return ms.ToArray();
}

Related

Add background to every page in PDF using iText 7

I have PDF file with one page and I want to use it like a background for all my pages in second PDF file with some information. I've tried to do it with CopyPagesTo but it just copy PDF every second page.
private void ApplyBackground(string sourceFilename, string backgroundPdf, int pageNumber) {
PdfDocument srcDocument = new PdfDocument(new PdfReader(sourceFilename));
PdfDocument bgDocument = new PdfDocument(new PdfReader(backgroundPdf));
PdfDocument destDocument = new PdfDocument(new PdfWriter(#"C:\Desktop\result.pdf").SetSmartMode(true));
int pagesCount = srcDocument.GetNumberOfPages();
for (int i = 1; i <= pagesCount; i++) {
srcDocument.CopyPagesTo(i, i, destDocument);
bgDocument.CopyPagesTo(1, 1, destDocument);
}
srcDocument.Close();
bgDocument.Close();
destDocument.Close();
}
Is it possible to use one PDF file like a background and put it into other PDF file every page behind text.
Here is the iText 7 code. Please note that it assumes equal page sizes for the page with the background and the pages of the document being processed.
PdfDocument backgroundDocument = new PdfDocument(new PdfReader(#"path/to/background_doc.pdf"));
PdfDocument pdfDocument = new PdfDocument(new PdfReader(#"path/to/source.pdf"),
new PdfWriter(#"path/to/target.pdf"));
PdfFormXObject backgroundXObject = backgroundDocument.GetPage(1).CopyAsFormXObject(pdfDocument);
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++) {
PdfPage page = pdfDocument.GetPage(i);
PdfStream stream = page.NewContentStreamBefore();
new PdfCanvas(stream, page.GetResources(), pdfDocument).AddXObject(backgroundXObject, 0, 0);
}
pdfDocument.Close();
backgroundDocument.Close();
Based on my understanding you are looking for below solution. If I missed something then please let me know.
Create reader for Original PDF for which you want to create background.
Create PDF reader for background PDF
Create PDF stamper where you want to generate final PDF.
Get background using GetImportedPage method for Stamper.
Loop on all pages of original PDF pages and add background.
Below is the code:
static void CreatePdfwithBackGround(string originalPdf, string backgroundPdf, string destPdf)
{
PdfReader originalPdfReader = new PdfReader(originalPdf);
PdfReader backgroundPdfReader = new PdfReader(backgroundPdf);
// Create the stamper for Destination pdf
PdfStamper stamper = new PdfStamper(originalPdfReader, new FileStream(destPdf, FileMode.Create));
// Add the backgroundPdf to each page of original PDF
PdfImportedPage page = stamper.GetImportedPage(backgroundPdfReader, 1);
int pageCount = originalPdfReader.NumberOfPages;
PdfContentByte background;
for (int i = 1; i <= pageCount; i++)
{
background = stamper.GetUnderContent(i);
background.AddTemplate(page, 0, 0);
}
// Close the Destination stamper
stamper.Close();
}
And example call is:
CreatePdfwithBackGround(#"C:\TEST\MainPDF.pdf", #"C:\TEST\BackGroundTemplate.pdf", #"C:\TEST\FinalPDFOutput.pdf");

how to add page footer to PDF document using iTextSharp for pages generated in landscape

From my ASP.net MVC application, I am generating PDFs using iTextSharp and XSLT as the template. The pages are supposed to be landscape oriented.
The PDF document in landscape mode by following.
new Document(PageSize.LETTER.Rotate(), marginLeft, marginRight, marginTop, marginBottom);
From the XSLT I get the HTML and construct PDF content as following:
public void ParseXhtmlContents(string xhtml)
{
//Instantiate handler
var elementhandler = new ElementHandler();
//Bind a reader to text
using (TextReader sr = new StringReader(xhtml))
{
//Parse
XMLWorkerHelper.GetInstance().ParseXHtml(elementhandler, sr);
}
//Loop through each element
foreach (var element in elementhandler.Elements)
{
var div = element as PdfDiv;
if (div != null)
foreach (var table in div.Content.OfType<PdfPTable>())
{
table.HeaderRows = 1;
}
_iTextDocument.Add(element);
}
}
After generating the PDF bytes, I am trying to add page footer as following
private byte[] AddPageHeader(byte[] pdf, float marginLeft, float marginRight, float marginTop, float marginBottom)
{
using (var stream = new MemoryStream())
{
stream.Write(pdf, 0, pdf.Length);
var reader = new PdfReader(pdf);
var totalPage = reader.NumberOfPages;
var pageSize = reader.GetPageSize(1);
var document = new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom);
var writer = PdfWriter.GetInstance(document, stream);
document.Open();
var contentByte = writer.DirectContent;
var pageIndex = 0;
for (var page = 1; page <= reader.NumberOfPages; page++)
{
document.NewPage();
pageIndex++;
var importedPage = writer.GetImportedPage(reader, page);
contentByte.AddTemplate(importedPage, 0, 0);
contentByte.BeginText();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
contentByte.SetFontAndSize(baseFont, 6);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "- " + pageIndex.ToString() + " -", 300f, 20, 0);
contentByte.EndText();
contentByte.SaveState();
contentByte.SetColorStroke(new PdfSpotColor("black", new BaseColor(0, 0, 0)), 100);
contentByte.SetLineWidth(0.25f);
contentByte.Rectangle(20, 45, 572, 0.25f);
contentByte.FillStroke();
contentByte.RestoreState();
}
startingPageNumber = pageIndex;
document.Close();
return stream.ToArray();
}
}
If I skip the part where I add page number I am able to generate the PDF in landscape orientation with content displayed in right order, however when I add footer I am getting the following result, the rendered page is in portrait.
Please refer the attached image for output:
Help me out on where I am going wrong, thanks.
I found what was wrong
I changed the document creating code as following:
var document = new Document(reader.GetPageSizeWithRotation(1), marginLeft, marginRight, marginTop, marginBottom);
and added a footer template similar to the above:
contentByte.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageIndex).Height);

how to add pagenumbers to every pdf page using itextsharp

here is What i want i want to add page numbers to every pdf page that i generated on the fly.
i used on end page method but it did not worked out even when i added the doc bottom margin.
I decided to add the page numbers after the pdf is generated from the file path.
here is my code for generating pdf:
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("t5.pdf", FileMode.Create));
doc.Open();//Open Document to write
iTextSharp.text.Font font8 = FontFactory.GetFont("ARIAL", 7);
Paragraph paragraph = new Paragraph("Some content");
doc.Add(paragraph);
doc.Add(paragraph);// add paragraph to the document
doc.Close();
FileStream stream = File.OpenRead("t5.pdf");
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
AddPageNumbers(fileBytes);
using (Stream file = File.OpenWrite("t5.pdf"))
{
file.Write(fileBytes, 0, fileBytes.Length);
}
}
and her is my add pagenumbers method:
MemoryStream ms = new MemoryStream();
PdfReader reader = new PdfReader(pdf);
int n = reader.NumberOfPages;
iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
Document document = new Document(psize, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();
PdfContentByte cb = writer.DirectContent;
int p = 0;
for (int page = 1; page <= reader.NumberOfPages; page++)
{
document.NewPage();
p++;
PdfImportedPage importedPage = writer.GetImportedPage(reader, page);
cb.AddTemplate(importedPage, 0, 0);
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.BeginText();
cb.SetFontAndSize(bf, 10);
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, +p + "/" + n, 100, 450, 0);
cb.EndText();
}
document.Close();
return ms.ToArray();
how ever it does not add the page numbers to the pdf document so what is the alternatives here? what can i do.
When posting a question here, please only post the smallest amount of code possible. Your "create a sample PDF with multiple pages" is 116 lines long. Inside of it you've got complicated PdfPTable and DataTable logic that is 100% unrelated to the problem. Instead, the following 13 lines is enough to make a multiple page PDF:
//Create a sample multiple page PDF and place it on the desktop
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "t5.pdf");
using (var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
for (var i = 0; i < 1000; i++) {
doc.Add(new Paragraph(String.Format("This is paragraph #{0}", i)));
}
doc.Close();
}
}
}
Second, get rid of try/catch. Those are great for production (sometimes) but at the development level that's why we have IDEs and compilers, they'll tell us specifically what's wrong.
Now on to the bigger problem, you need to keep these two processes separate from each other. Every single brace and object from part part #1 must be closed, done and accounted for. Part #2 then needs to be fed a completely valid PDF but neither of the two parts should be "aware" of each other or depend on each other.
Since you just borrowed some code that wasn't intended for what you're trying to do I'm going to also ignore that and use some code that I know specifically will work. Also, since you're open to using a MemoryStream in the first place I'm just going to avoid writing to disk until I need to. Below is a full working sample that creates a multiple page and then adds page numbers in a second pass.
//Will hold our PDF as a byte array
Byte[] bytes;
//Create a sample multiple page PDF, nothing special here
using (var ms = new MemoryStream()) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, ms)) {
doc.Open();
for (var i = 0; i < 1000; i++) {
doc.Add(new Paragraph(String.Format("This is paragraph #{0}", i)));
}
doc.Close();
}
}
//Store our bytes before
bytes = ms.ToArray();
}
//Read our sample PDF and apply page numbers
using (var reader = new PdfReader(bytes)) {
using (var ms = new MemoryStream()) {
using (var stamper = new PdfStamper(reader, ms)) {
int PageCount = reader.NumberOfPages;
for (int i = 1; i <= PageCount; i++) {
ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_CENTER, new Phrase(String.Format("Page {0} of {1}", i, PageCount)), 100, 10 , 0);
}
}
bytes = ms.ToArray();
}
}
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "t5.pdf");
System.IO.File.WriteAllBytes(outputFile, bytes);

Adding text to each page of PDF with itext instead of just the first page

I'm trying to add text to each page of a PDF using this code which works great, but this code is only for adding text to the first page:
//variables
String pathin = #"C:\Users\root\Desktop\temp\test.pdf";
String pathout = #"C:\Users\root\Desktop\temp\test2.pdf";
//create a document object
//var doc = new Document(PageSize.A4);
//create PdfReader object to read from the existing document
PdfReader reader = new PdfReader(pathin);
//create PdfStamper object to write to get the pages from reader
PdfStamper stamper=new PdfStamper(reader, new FileStream(pathout, FileMode.Create));
// PdfContentByte from stamper to add content to the pages over the original content
PdfContentByte pbover = stamper.GetOverContent(1);
//add content to the page using ColumnText
ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase("Hello World"), 10, 10, 0);
// PdfContentByte from stamper to add content to the pages under the original content
//PdfContentByte pbunder = stamper.GetUnderContent(1);
//close the stamper
stamper.Close();
I've seen examples using:
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var importedPage = writer.GetImportedPage(reader, i);
To iterate through each page, but I'm having trouble tying that into my code above. Any helps would be much appreciated, thanks.
in your first snippet, you hardcode the page number:
stamper.GetUnderContent(1);
In your second snippet, you loop over the pages:
for (var i = 1; i <= reader.NumberOfPages; i++) {
}
Now combine these two snippets:
for (var i = 1; i <= reader.NumberOfPages; i++) {
PdfContentByte pbunder = stamper.GetUnderContent(i);
// do stuff with bunder
}

Stack overflow exception on itextsharp document.close

I am merging multiple PDF into one single file.
here is my code:
string[] files = Directory.GetFiles(FolderName);
using (FileStream fs = new FileStream(PdfOutputFile, FileMode.Create))
{
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
foreach (string percorsoFile in files)
{
PdfContentByte cb = writer.DirectContent;
PdfReader reader = new PdfReader(percorsoFile);
int numpagine = reader.NumberOfPages - PageAddedByWord;
for (int pagina = 1; pagina <= numpagine; pagina++)
{
int rotazione = reader.GetPageSizeWithRotation(pagina).Rotation;
PdfImportedPage page = writer.GetImportedPage(reader, pagina);
doc.NewPage();
if (rotazione == 90 || rotazione == 270)
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pagina).Height);
else
cb.AddTemplate(page, 0f, 0f);
}
reader.Close();
}
doc.Close();
}
And I get stack overflow exception on the doc.Close() line, but I can't understand why. I close every reader as soon as I am done with that file. I get this exception only with large sets of pdf (like 2000).
thank's for the help.
SOLVED, I used PdfSmartCopy instead of pdfwriter. Now the code looks like this
Document doc = new Document();
PdfSmartCopy pdfCopy = new PdfSmartCopy(doc, ms);
doc.Open();
foreach (var percorsoFilePdf in files)
{
PdfReader reader = new PdfReader(percorsoFilePdf);
int numpagine = reader.NumberOfPages;
for (int I = 1; I <= numpagine-PageAddedByWord; I++)
{
doc.SetPageSize(reader.GetPageSizeWithRotation(1));
PdfImportedPage page = pdfCopy.GetImportedPage(reader, I);
pdfCopy.AddPage(page);
}
//Clean up
//pdfCopy.FreeReader(reader);
reader.Close();
}
//Clean up
doc.Close();
SharedMethods.MemoryStreamToFile(ms, PdfOutputFile);
also using a memorystream instead of a file stream is much faster, and the process doesn't get "too large".

Categories