PdfBox-Android adding page text overlay - java

Stuck with PDFBox-Android, pdfbox-android:1.8.9.0
I load first page of a pdf file, write text in and import this page to a new page of a final document.
Problem is when create new pages, it use last page which contain previous text...
So, first page is ok, but nexts have texts superposed..
private File writeReport() {
File fileSource = new File(getActivity().getApplicationContext().getExternalCacheDir(), "fileSource.pdf");
// get file model in assets
InputStream inputAsset = null;
try {
inputAsset = getActivity().getApplicationContext().getResources().getAssets().open("file_model.pdf");
} catch (IOException e) {
e.printStackTrace();
}
// copy file model in fileSource
try {
OutputStream outputStream = new FileOutputStream(file);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = inputAsset.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputAsset.close();
} catch (IOException e) {
System.out.print("Copy assets : IOException" + e.getMessage());
}
File fileTarget = new File(getActivity().getApplicationContext().getCacheDir(), "fileTarget.pdf");
try {
PDFBoxResourceLoader.init(getActivity().getApplicationContext()); // init lib
PDDocument documentSource = PDDocument.load(fileSource);
PDDocument documentTarget = new PDDocument();
// iteration == a new page
for(int i=0 ; i < 3 ; i++)
{
PDPage page = documentSource.getDocumentCatalog().getPages().get(0);
PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true);
PDFont font1 = PDType1Font.HELVETICA;
float startY = page.getMediaBox().getUpperRightY();
float factor = 2.83f;
page.getStream();
// add text
contentStream.beginText();
contentStream.setFont(font1, 10);
contentStream.newLineAtOffset(factor * 60, startY - (factor * 53));
contentStream.showText("test text" + i);
contentStream.endText();
contentStream.close();
// import source page to output file-> Problem here ! new page contain old overlay text...
documentTarget.importPage(page);
}
documentTarget.save(fileTarget);
documentTarget.close();
documentSource.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileTarget;
}
Is there a way to have fresh page at every iteration ?
Thanks !

The problem is that you reused the same PDPage object, which resulted in the effect that it contained the text of the previous iteration. Solution: use the result of
documentTarget.importPage(page)
and work with that one. Because that is a new PDPage object with everything cloned. So your new code will be like this:
// iteration == a new page
for(int i=0 ; i < 3 ; i++)
{
PDPage page = documentSource.getDocumentCatalog().getPages().get(0);
page = documentTarget.importPage(page); // this is now a new PDPage object
PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true);
PDFont font1 = PDType1Font.HELVETICA;
float startY = page.getMediaBox().getUpperRightY();
float factor = 2.83f;
page.getStream();
// add text
contentStream.beginText();
contentStream.setFont(font1, 10);
contentStream.newLineAtOffset(factor * 60, startY - (factor * 53));
contentStream.showText("test text" + i);
contentStream.endText();
contentStream.close();
}

Related

PDF File generated by PdfWriter is showing blank page

I have code to merge between 2 pdf files using the library OpenPDF like this
public byte[] mergePDF(byte[] pdf1, byte[] pdf2) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer;
try {
writer = PdfWriter.getInstance(document, os);
} catch (DocumentException e) {
throw new IllegalStateException(e);
}
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfReader pdfReader1 = new PdfReader(pdf1);
PdfReader pdfReader2 = new PdfReader(pdf2);
for (int i = 0; i < pdfReader1.getNumberOfPages(); i++) {
PdfImportedPage page = writer.getImportedPage(pdfReader1, i + 1);
document.setPageSize(pdfReader1.getPageSize(i + 1));
document.newPage();
cb.addTemplate(page, 0, 0);
}
for (int i = 0; i < pdfReader2.getNumberOfPages(); i++) {
PdfImportedPage page = writer.getImportedPage(pdfReader2, i + 1);
document.setPageSize(pdfReader2.getPageSize(i + 1));
document.newPage();
cb.addTemplate(page, 0, 0);
}
document.close();
return os.toByteArray();
}
Merged PDF generated showing a blank page from on Page come from the second pdf but it showing when open with adobe acrobat, firefox, and ubuntu document viewer. Does anyone know what issue? or any missing configuration that needs to be set in my code?

Merging PDF using Itext [duplicate]

This question already has answers here:
Merge pdf documents of different width using iText
(2 answers)
Closed 7 years ago.
I am encountering an issue while merging two PDFs generated out of IText.
Code snippet of PDF Creation:
Document configuration is as below:
iText_xls_2_pdf = new Document(PageSize.A4.rotate(),10,0,30,0);
Using this configuration 2 PDFs are created.
After creation of these PDFs, they need to be merged as single pdf.
Below code is used to merge the PDFs:
Document document = new Document(PageSize.A4.rotate(),10,0,30,0);
try {
List<InputStream> pdfs = streamOfPDFFiles;
List<PdfReader> readers = new ArrayList<PdfReader>();
int totalPages = 0;
Iterator<InputStream> iteratorPDFs = pdfs.iterator();
// Create Readers for the pdfs.
while (iteratorPDFs.hasNext()) {
InputStream pdf = iteratorPDFs.next();
PdfReader pdfReader = new PdfReader(pdf);
readers.add(pdfReader);
totalPages += pdfReader.getNumberOfPages();
}
// Create a writer for the outputstream
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
// data
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();
// Loop through the PDF files and add to the output.
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader,
pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
// Code for pagination.
if (paginate) {
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
+ currentPageNumber + " of " + totalPages, 520,
5, 0);
cb.endText();
}
}
pageOfCurrentReaderPDF = 0;
}
outputStream.flush();
document.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document.isOpen())
document.close();
try {
if (outputStream != null)
outputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
This creates the merged PDF. but Because I have used .rotate in Document configuration, Merged pdf comes in landscape format. How could I avoid this.
Please take a look at table 6.1 in chapter 6 of my book, you'll find out that using PdfWriter to merge documents is not done. If you want to merge documents, you need to use PdfCopy.
For an example, see for instance these questions:
How to do pdf writing and copying at once?
put page number when create PDF with iTextSharp
Edit DirectContent of iTextSharp PdfSmartCopy class
...
(As you can see, this question has been answered many times before...)
An example:
PdfReader reader1 = new PdfReader(file1);
PdfReader reader2 = new PdfReader(file2);
Document document = new Document();
FileOutputStream fos = new FileOutputStream();
PdfCopy copy = new PdfCopy(document, fs);
document.open();
PdfImportedPage page;
PdfCopy.PageStamp stamp;
Phrase phrase;
BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 9);
int n = reader1.getNumberOfPages();
for (int i = 1; i <= reader1.getNumberOfPages(); i++) {
page = copy.getImportedPage(reader1, i);
stamp = copy.createPageStamp(page);
phrase = new Phrase("page " + i, font);
ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, phrase, 520, 5, 0);
stamp.alterContents();
copy.addPage(page);
}
for (int i = 1; i <= reader2.getNumberOfPages(); i++) {
page = copy.getImportedPage(reader2, i);
stamp = copy.createPageStamp(page);
phrase = new Phrase("page " + (n + i), font);
ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, phrase, 520, 5, 0);
stamp.alterContents();
copy.addPage(page);
}
document.close();
reader1.close();
reader2.close();

Java: iText library PDF merging, pdf always on the bottom of the page

I am using iText library for merging individual pdfs. But the individual pdf always come on the bottom of the page. Any pointer how I can make it to display in the middle or on teh top. I have adjusted my code from http://viralpatel.net/blogs/itext-tutorial-merge-split-pdf-files-using-itext-jar/
public static void concatPDFs(OutputStream outputStream, boolean paginate) {
Document document = new Document();
try {
List<InputStream> pdfs = listPdf;
List<PdfReader> readers = new ArrayList<PdfReader>();
int totalPages = 0;
Iterator<InputStream> iteratorPDFs = pdfs.iterator();
// Create Readers for the pdfs.
while (iteratorPDFs.hasNext()) {
InputStream pdf = iteratorPDFs.next();
PdfReader pdfReader = new PdfReader(pdf);
readers.add(pdfReader);
totalPages += pdfReader.getNumberOfPages();
}
// Create a writer for the outputstream
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
// data
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();
// Loop through the PDF files and add to the output.
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader,
pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
// Code for pagination.
if (paginate) {
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
+ currentPageNumber + " of " + totalPages, 520,
5, 0);
cb.endText();
}
}
pageOfCurrentReaderPDF = 0;
}
outputStream.flush();
document.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document.isOpen())
document.close();
try {
if (outputStream != null)
outputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
First to clear out about coordinate system. PDF coordinate system (0, 0) represents the bottom-left corner of the PDF page.
You can calculate new 'y' position for inserting page template like this:
y = new document heigh - imported page height
and then you can change your code to:
cb.addTemplate(page, 0, y);
The best you can do to merge PDF files is follow the intructions of the iText developer..
https://stackoverflow.com/a/24359217/2865036
Doing like he said, the format of your PDF files will not change

How to add a page number to the output pdf when merging two pdfs?

I am using the following code to merge two pdfs:
File firstPdfFile = new File("firstPdf.pdf");
File secondPdfFile = new File("secondPdf.pdf");
PDFMergerUtility merger = new PDFMergerUtility();
merger.addSource(firstPdfFile);
merger.addSource(secondPdfFile);
String pdfPath = "PdfFile.pdf";
OutputStream bout2 = new BufferedOutputStream(new FileOutputStream(pdfPath));
merger.setDestinationStream(bout2);
merger.mergeDocuments();
File pdfFile = new File(pdfPath);
I am getting the merged pdf correctly but I want to add page number in this pdf file.
Try this code.
File firstPdfFile = new File("firstPdf.pdf");
File secondPdfFile = new File("firstPdf.pdf");
PDFMergerUtility merger = new PDFMergerUtility();
merger.addSource(firstPdfFile);
merger.addSource(secondPdfFile);
String pdfPath = "PdfFile.pdf";
OutputStream bout2 = new BufferedOutputStream(new FileOutputStream(pdfPath));
merger.setDestinationStream(bout2);
merger.mergeDocuments();
PDDocument doc = null;
try {
URL file = new URL("file:///PdfFile.pdf");
doc = PDDocument.load(file);
List<?> allPages = doc.getDocumentCatalog().getAllPages();
PDFont font = PDType1Font.HELVETICA_BOLD;
float fontSize = 36.0f;
for (int i = 0; i < allPages.size(); i++) {
PDPage page = (PDPage) allPages.get(i);
PDPageContentStream footercontentStream = new PDPageContentStream(doc, page, true, true);
footercontentStream.beginText();
footercontentStream.setFont(font, fontSize);
footercontentStream.moveTextPositionByAmount((PDPage.PAGE_SIZE_A4.getUpperRightX() / 2), (PDPage.PAGE_SIZE_A4.getLowerLeftY()));
footercontentStream.drawString(String.valueOf(i + 1));
footercontentStream.endText();
footercontentStream.close();
}
doc.save("PdfFile.pdf");
} finally {
if (doc != null) {
doc.close();
}
}
Try below code for PDFBox 2.0
public class PageNumberExample {
final boolean isCompress = false;
final boolean isContextReset = true;
public static void main(String[] args) throws IOException {
new PageNumberExample().addPageNumber("merged PDF path");
}
public void addPageNumber(String pdfPath) throws IOException {
File mergePpdfFile = new File(pdfPath);
PDDocument document = PDDocument.load(mergePpdfFile);
int totalPage = document.getNumberOfPages();
for(int i=0; i<totalPage; i++) {
PDPage page = document.getPage(i);
PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, isCompress, isContextReset);
stream.setNonStrokingColor(Color.BLACK);
stream.beginText();
stream.setFont(PDType1Font.COURIER, 10);
stream.newLineAtOffset(100, 100); //Set position where you want to print page number.
stream.showText("Page " + (i+1) + " of " + totalPage);
stream.endText();
stream.close();
}
document.save(pdfPath);
document.close();
}
}

Itext pdf Merge : Document overflow outside pdf (Text truncated) page and not displaying

i am trying to merge 2 pdf in one. Merging is working fine but contents overflow from pdf page. A shown in attachment. Original Document pdf is as Follows.
After Merge Document is coming like this
Java code as follows :
BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
//BaseFont bf= BaseFont.createFont();
PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
// data
PdfImportedPage page;
int currentPageNumber = 0;
int pageOfCurrentReaderPDF = 0;
Iterator<PdfReader> iteratorPDFReader = readers.iterator();
// Loop through the PDF files and add to the output.
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader,
pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
// Code for pagination.
if (paginate) {
cb.beginText();
cb.setFontAndSize(bf, 9);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
+ currentPageNumber + " of " + totalPages, 520,
5, 0);
cb.endText();
}
}
pageOfCurrentReaderPDF = 0;
}
please Help.
Please download chapter 6 of my book and take a look at table 6.1. You're making the mistake merging two documents using PdfWriter instead of using PdfCopy as documented. Take a look at listing 6.22 to find out how to add page numbers when using PdfCopy.
i used "PdfCopyFields" Snippet as follows :
public static boolean concatPDFFiles(List<String> listOfFiles,
String outputfilepath) throws FileNotFoundException, DocumentException {
PdfCopyFields copy = null;
try {
copy = new PdfCopyFields(new FileOutputStream(outputfilepath));
} catch (DocumentException ex) {
Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
}
try {
for (String fileName : listOfFiles) {
PdfReader reader1 = new PdfReader(fileName);
copy.addDocument(reader1);
}
} catch (IOException ex) {
Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
} finally {
copy.close();
}
if (new File(outputfilepath).exists()) {
double bytes = new File(outputfilepath).length();
//double kilobytes = (bytes / 1024);
if (bytes != 0) {
return true;
} else {
return false;
}
} else {
return false;
}
}

Categories