Itextsharp PdfDocument or PdfDocument from It.Document
By : Tom
Date : March 29 2020, 07:55 AM
should help you out I am adding page numbers to the bottom of a pdf document using ITextSharp The thing is, it is made up of 4 or 5 different PDF's that are combined through the process, and there are some dynamically added pages so the PDF can vary in size. I cannot call document.PageCount because it "lacks the get accessor". , Found it guys :D So whenever you make your writer: code :
using (MemoryStream MS = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(doc, MS);
writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
PdfFooter eventHandler = new PdfFooter();
writer.PageEvent = eventHandler;
ect.
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text.pdf;
using iTextSharp.text;
public class PdfFooter : PdfPageEventHelper
{
// This is the contentbyte object of the writer
PdfContentByte cb;
// we will put the final number of pages in a template
PdfTemplate template;
// this is the BaseFont we are going to use for the header / footer
BaseFont bf = null;
// This keeps track of the creation time
DateTime PrintTime = DateTime.Now;
#region Properties
private string _Title;
public string Title
{
get { return _Title; }
set { _Title = value; }
}
private string _HeaderLeft;
public string HeaderLeft
{
get { return _HeaderLeft; }
set { _HeaderLeft = value; }
}
private string _HeaderRight;
public string HeaderRight
{
get { return _HeaderRight; }
set { _HeaderRight = value; }
}
private Font _HeaderFont;
public Font HeaderFont
{
get { return _HeaderFont; }
set { _HeaderFont = value; }
}
private Font _FooterFont;
public Font FooterFont
{
get { return _FooterFont; }
set { _FooterFont = value; }
}
#endregion
// we override the onOpenDocument method
public override void OnOpenDocument(PdfWriter writer, Document document)
{
try
{
PrintTime = DateTime.Now;
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb = writer.DirectContent;
template = cb.CreateTemplate(50, 50);
}
catch (DocumentException de)
{
}
catch (System.IO.IOException ioe)
{
}
}
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
int pageN = writer.PageNumber;
String text = "Page " + pageN + " of ";
float len = bf.GetWidthPoint(text, 8);
Rectangle pageSize = document.PageSize;
cb.SetRGBColorFill(100, 100, 100);
cb.BeginText();
cb.SetFontAndSize(bf, 8);
cb.SetTextMatrix(pageSize.GetRight(70), pageSize.GetBottom(15));
cb.ShowText(text);
cb.EndText();
cb.AddTemplate(template, pageSize.GetRight(70) + len, pageSize.GetBottom(15));
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
template.BeginText();
template.SetFontAndSize(bf, 8);
template.SetTextMatrix(0, 0);
template.ShowText("" + (writer.PageNumber - 1));
template.EndText();
}
|
How to copy one page of a PDFDocument , to another PDFDocument
By : Jay K
Date : March 29 2020, 07:55 AM
may help you . A page in a PDF has many relations to other objects in a PDF. If you could add a page located in one document to another one, the page would reside in both documents. Thus, the page suddenly would have to have all those relations to objects in both documents. This obviously does not work, thus iText prevents this. code :
PdfDocument pdfWithMultiplePages = here I have a PDF with 3 pages.
....
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
pdfDocument.copyPagesTo(i+1, i+1,pdfWithMultiplePages);
pdfWithMultiplePages.copyPagesTo(i+1, i+1, pdfDocument);
|
How to merge List<PdfDocument> into a single PdfDocument
By : Ernesto
Date : March 29 2020, 07:55 AM
I hope this helps . I have a List with multiple PDFs , and I want to merge all of them into a single PdfDocument (im using iText) , and then transform this PdfDocument into a ByteArrayOutputStream (OR a byte[]). code :
public byte[] mergePdfDocumentsIntoAPdfDocument(List<PdfDocument> pdfDocuments){
ByteArrayOutputStream mergedPdfStream = new ByteArrayOutputStream();
PdfDocument resultDoc = new PdfDocument(new PdfWriter(mergedPdfStream));
for (PdfDocument doc : pdfDocuments) {
int n = doc.getNumberOfPages();
for (int i = 1; i <= n; i++) {
PdfPage page = doc.getPage(i).copyTo(resultDoc);
resultDoc.addPage(page);
}
}
resultDoc.close();
return mergedPdfStream.toByteArray();
}
|
Cocoa osx PDFView NSPrintOperation PrintPanel not showing page preview
By : Bowen Noack
Date : March 29 2020, 07:55 AM
I wish did fix the issue. From this link:
|
Html to PDF using PdfDocument
By : nidhi patel
Date : March 29 2020, 07:55 AM
hope this fix your issue This class does what you are looking for. But to compile, it must be in package ".../java/android/print/"
|