PDFBox 2.0.9 - creating TOC with multiple pages - java

I'm working on PDF Toc. It generates the first page but when I have more elements I did logic to create a new page for TOC. I'm using PDF Box and that PDPageContentStream.
I had to create a function to calculate how many pages I need. Then I'm creating the exact amount of pages in the list and add them to the PDF document before I start PDPageContentStream. That stream is in a loop and it's only generating the first page. Other pages come blank. I don't know what exactly is wrong. Here is the code:
PDDocument pdf = new PDDocument();
int numberOfTocPages = calculateTocPages(50, tocStyle, _height, currentYPosition, leading);
List<PDPage> tocPages = new ArrayList<PDPage>();
for (int i = 0; i < numberOfTocPages; i++) {
PDPage toc = new PDPage(new PDRectangle(_width, _height));
tocPages.add(toc);
}
float numberingXPosition = _width - (contentMarginLeft + contentMarginRight) - 100;
int j = 0;
PDDocument temp = new PDDocument();
for (int i = 0; i < numberOfTocPages; i++) {
PDPage toc = tocPages.get(i);
pdf.addPage(toc);
try {
PDPageContentStream contentStream = new PDPageContentStream(pdf, toc, PDPageContentStream.AppendMode.APPEND, true, false);
contentStream.beginText();
if (i == 0) {
contentStream.setFont(font, fontSize);
contentStream.setNonStrokingColor(tocStyle.getHeaderFontColor());
contentStream.newLineAtOffset(headerMarginLeft, currentYPosition);
contentStream.showText(StylingEnums.TABLE_OF_CONTENTS);
}
fontSize = tocStyle.getContentFontSize();
leading = 1.5f * fontSize;
contentStream.setFont(PDType1Font.HELVETICA, fontSize);
contentStream.setNonStrokingColor(tocStyle.getContentFontColor());
contentStream.newLineAtOffset(contentMarginLeft, -leading);
currentYPosition -= leading;
contentStream.setLeading(leading);
// List<TocContentData> tocItems = _tocData.getTocItems();
while (j < 50) {
if (currentYPosition >= 2 * tocStyle.getContentMarginBottom()) {
// TocContentData item = tocItems.get(i);
contentStream.showText("Item " + j);
contentStream.newLineAtOffset(numberingXPosition, 0);
int pageNumber = j;
contentStream.showText(Integer.toString(pageNumber + 1));
contentStream.newLineAtOffset(-numberingXPosition, 0);
contentStream.newLine();
currentYPosition -= leading;
j++;
} else {
System.out.println("New page!!!");
currentYPosition = initialPosition;
break;
}
}
contentStream.endText();
contentStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream out;
try {
out = new FileOutputStream(fileName + ".pdf");
pdf.save(out);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Page 2 sets the Y offset with this code because currentYPosition is not used when i > 0
contentStream.newLineAtOffset(contentMarginLeft, -leading);
so your first Y offset value is negative.

Related

PDFBOX 2.0+ java flatten annotations freetext created by foxit

I ran into a very tough issue. We have forms that were supposed to be filled out, but some people used annotation freeform text comments in foxit instead of filling the form fields, so the annotations never flatten. When our render software generates the final document annotations are not included.
The solution I tried is to basically go through the document, get the annotation text content and write it to the pdf so it is on the final document then remove the actual annotation, but I run into an issue where I don't know the font the annotation is using, line space, etc so cannot find out how to get it from a pdfbox to recreate exacactly as the annotation looks on the unflattened form.
Basically I want to flatten annotatations that are freeform created in foxit (The typewriter comment feature)
Here is the code. It is working, but again I am struggling with figuring out how to get the annotations to write to my final pdf document. Again flatten on the acroform is not working because these are not acroform fields! The live code filters out anything that is not a freetext type annotation, but below code should show my issue.
public static void main(String [] args)
{
String startDoc = "C:/test2/test.pdf";
String finalFlat = "C:/test2/test_FLAT.pdf";
try {
// for testing
try {
//BasicConfigurator.configure();
File myFile = new File(startDoc);
PDDocument pdDoc = PDDocument.load( myFile );
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
// set the NeedApperances flag
pdAcroForm.setNeedAppearances(false);
// correct the missing page link for the annotations
for (PDPage page : pdDoc.getPages()) {
for (PDAnnotation annot : page.getAnnotations()) {
System.out.println(annot.getContents());
System.out.println(annot.isPrinted());
System.out.println(annot.isLocked());
System.out.println(annot.getAppearance().toString());
PDPageContentStream contentStream = new PDPageContentStream(pdDoc, page, PDPageContentStream.AppendMode.APPEND,true,true);
int fontHeight = 14;
contentStream.setFont(PDType1Font.TIMES_ROMAN, fontHeight);
float height = annot.getRectangle().getLowerLeftY();
String s = annot.getContents().replaceAll("\t", " ");
String ss[] = s.split("\\r");
for(String sss : ss)
{
contentStream.beginText();
contentStream.newLineAtOffset(annot.getRectangle().getLowerLeftX(),height );
contentStream.showText(sss);
height = height + fontHeight * 2 ;
contentStream.endText();
}
contentStream.close();
page.getAnnotations().remove(annot);
}
}
pdAcroForm.flatten();
pdDoc.save(finalFlat);
pdDoc.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e) {
System.err.println("Exception: " + e.getLocalizedMessage());
}
}
This was not a fun one. After a million different tests, and I STILL do not understand all the nuances, but this is the version that appeas to flatten all pdf files and annotations if they are visible on PDF. Tested about half a dozen pdf creators and if an annotation is visible on a page this hopefully flattens it. I suspect there is a better way by pulling the matrix and transforming it and what not, but this is the only way I got it to work everywhere.
public static void flattenv3(String startDoc, String endDoc) {
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
String finalFlat = endDoc;
try {
try {
//BasicConfigurator.configure();
File myFile = new File(startDoc);
PDDocument pdDoc = PDDocument.load(myFile);
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
if (pdAcroForm != null) {
pdAcroForm.setNeedAppearances(false);
pdAcroForm.flatten();
}
// set the NeedApperances flag
boolean isContentStreamWrapped;
int ii = 0;
for (PDPage page: pdDoc.getPages()) {
PDPageContentStream contentStream;
isContentStreamWrapped = false;
List < PDAnnotation > annotations = new ArrayList < > ();
for (PDAnnotation annotation: page.getAnnotations()) {
if (!annotation.isInvisible() && !annotation.isHidden() && annotation.getNormalAppearanceStream() != null)
{
ii++;
if (ii > 1) {
// contentStream.close();
// continue;
}
if (!isContentStreamWrapped) {
contentStream = new PDPageContentStream(pdDoc, page, AppendMode.APPEND, true, true);
isContentStreamWrapped = true;
} else {
contentStream = new PDPageContentStream(pdDoc, page, AppendMode.APPEND, true);
}
PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
PDFormXObject fieldObject = new PDFormXObject(appearanceStream.getCOSObject());
contentStream.saveGraphicsState();
boolean needsTranslation = resolveNeedsTranslation(appearanceStream);
Matrix transformationMatrix = new Matrix();
boolean transformed = false;
float lowerLeftX = annotation.getNormalAppearanceStream().getBBox().getLowerLeftX();
float lowerLeftY = annotation.getNormalAppearanceStream().getBBox().getLowerLeftY();
PDRectangle bbox = appearanceStream.getBBox();
PDRectangle fieldRect = annotation.getRectangle();
float xScale = fieldRect.getWidth() - bbox.getWidth();
transformed = true;
lowerLeftX = fieldRect.getLowerLeftX();
lowerLeftY = fieldRect.getLowerLeftY();
if (bbox.getLowerLeftX() <= 0 && bbox.getLowerLeftY() < 0 && Math.abs(xScale) < 1) //BASICALLY EQUAL TO 0 WITH ROUNDING
{
lowerLeftY = fieldRect.getLowerLeftY() - bbox.getLowerLeftY();
if (bbox.getLowerLeftX() < 0 && bbox.getLowerLeftY() < 0) //THis is for the o
{
lowerLeftX = lowerLeftX - bbox.getLowerLeftX();
}
} else if (bbox.getLowerLeftX() == 0 && bbox.getLowerLeftY() < 0 && xScale >= 0) {
lowerLeftX = fieldRect.getUpperRightX();
} else if (bbox.getLowerLeftY() <= 0 && xScale >= 0) {
lowerLeftY = fieldRect.getLowerLeftY() - bbox.getLowerLeftY() - xScale;
} else if (bbox.getUpperRightY() <= 0) {
if (annotation.getNormalAppearanceStream().getMatrix().getShearY() < 0) {
lowerLeftY = fieldRect.getUpperRightY();
lowerLeftX = fieldRect.getUpperRightX();
}
} else {
}
transformationMatrix.translate(lowerLeftX,
lowerLeftY);
contentStream.transform(transformationMatrix);
contentStream.drawForm(fieldObject);
contentStream.restoreGraphicsState();
contentStream.close();
}
}
page.setAnnotations(annotations);
}
pdDoc.save(finalFlat);
pdDoc.close();
File file = new File(finalFlat);
// Desktop.getDesktop().browse(file.toURI());
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
System.err.println("Exception: " + e.getLocalizedMessage());
}
}
}

How to split a pdf into multiple pages in a single pdf using PDFBOX

Requirement:
My pdf has 5 pages, I need to start the split on the second page and end at the last page, in a single pdf. Currently, I have made it so that it splits one pdf per page.
Current code:
public static boolean SepararFC(String sequence, String pathfrom, String pathto) {
try (PDDocument document = PDDocument.load(new File(pathfrom))) {
Splitter splitter = new Splitter();
List<PDDocument> Pages = splitter.split(document);
for (int i = 0; i < Pages.size(); i++) {
PDDocument doc = Pages.get(i);
doc.save(new File(pathfrom, sequence + "_" + i + ".pdf"));
}
} catch (IOException e){
System.err.println(e);
}
return true;
Since you don't want page 1, you can just remove it and save a new file
try (PDDocument document = PDDocument.load(new File(pathfrom))) {
//Listing the number of existing pages
int noOfPages= document.getNumberOfPages();
System.out.print(noOfPages);
//Removing the pages
document.removePage(0);
System.out.println("page removed");
//Saving the document
document.save(new File(pathfrom, sequence + "new"+ ".pdf"));
//Closing the document
document.close();}
catch (IOException e){
System.err.println(e);
}
Assuming your code is correct:
public static boolean SepararFC(String sequence, String pathfrom, String pathto) {
int start = 1;
int end = 4;
try (PDDocument document = PDDocument.load(new File(pathfrom))) {
Splitter splitter = new Splitter();
List<PDDocument> Pages = splitter.split(document);
for (int i = 0; i < Pages.size(); i++) {
PDDocument doc = Pages.get(i);
if(i > start && i <= end){
doc.save(new File(pathfrom, sequence + "_" + i + ".pdf"));
}
}
} catch (IOException e){
System.err.println(e);
}
return true;

Add padding to table cells using iTextPdf

Following is a demo code to generate a PDF doc from a HTML source:
public class SimpleAdhocReport
{
public SimpleAdhocReport()
{
build();
}
private void build()
{
AdhocConfiguration configuration = new AdhocConfiguration();
AdhocReport report = new AdhocReport();
configuration.setReport(report);
AdhocColumn column = new AdhocColumn();
column.setName("item");
report.addColumn(column);
column = new AdhocColumn();
column.setName("orderdate");
report.addColumn(column);
column = new AdhocColumn();
column.setName("quantity");
report.addColumn(column);
column = new AdhocColumn();
column.setName("unitprice");
report.addColumn(column);
try
{
AdhocManager.saveConfiguration(configuration, new FileOutputStream("d:/configuration.xml"));
#SuppressWarnings("unused")
AdhocConfiguration loadedConfiguration = AdhocManager.loadConfiguration(new FileInputStream("d:/configuration.xml"));
JasperReportBuilder reportBuilder = AdhocManager.createReport(configuration.getReport());
reportBuilder.setDataSource(createDataSource());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
reportBuilder.toHtml(baos);
String html = new String(baos.toByteArray(), "UTF-8");
baos.close();
Whitelist wl = Whitelist.simpleText();
wl.addTags("table", "tr", "td");
String clean = Jsoup.clean(html, wl);
clean = clean.replace("<td></td>", "");
clean = clean.replace("<td> </td>", "");
clean = clean.replace("<td> ", "<td>");
Document doc = Jsoup.parse(clean);
for (Element element : doc.select("*"))
{
if (!element.hasText() && element.isBlock())
{
element.remove();
}
}
clean = doc.body().html();
int startIndex = clean.indexOf("<table>", 6);
int endIndex = clean.indexOf("</table>");
clean = clean.substring(startIndex, endIndex + 8);
BufferedWriter writer = new BufferedWriter(new FileWriter(("d:/test.html")));
writer.write(clean);
writer.close();
try
{
createPdf(clean);
}
catch (DocumentException e)
{
e.printStackTrace();
}
}
catch (DRException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private JRDataSource createDataSource()
{
DRDataSource dataSource = new DRDataSource("item", "orderdate", "quantity", "unitprice");
for (int i = 0; i < 20; i++)
{
dataSource.add("Book", new Date(), (int) (Math.random() * 10) + 1,
new BigDecimal(Math.random() * 100 + 1).setScale(4, BigDecimal.ROUND_HALF_UP));
}
return dataSource;
}
public static void main(String[] args)
{
new SimpleAdhocReport();
}
public void createPdf(String html) throws IOException, DocumentException
{
com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.LETTER);
document.setMargins(30, 30, 80, 30);
PdfWriter.getInstance(document, new FileOutputStream("D:\\HTMLtoPDF.pdf"));
document.open();
PdfPTable table = null;
ElementList list = com.itextpdf.tool.xml.XMLWorkerHelper.parseToElementList(html, null);
for (com.itextpdf.text.Element element : list)
{
table = new PdfPTable((PdfPTable) element);
}
table.setWidthPercentage(100);
ArrayList<PdfPRow> rows = table.getRows();
for (PdfPRow rw : rows)
{
PdfPCell[] cells = rw.getCells();
for (PdfPCell cl : cells)
{
cl.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
cl.setBorder(PdfPCell.NO_BORDER);
cl.setNoWrap(true);
cl.setPadding(10f);
cl.setCellEvent(new MyCell());
}
}
document.add(table);
document.close();
}
}
class MyCell implements PdfPCellEvent
{
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
float x1 = position.getLeft() - 2;
float x2 = position.getRight() + 2;
float y1 = position.getTop() + 2;
float y2 = position.getBottom() - 2;
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.rectangle(x1, y1, x2 - x1, y2 - y1);
canvas.stroke();
}
}
I am working with jasper reports to create a adhoc report and generate the HTML from there. I have to generate a PDF from this HTML.
Couple of issue I am facing, any help is appreciated:
I am setting the
table.setWidthPercentage(100);
for a page with table its not working.
I have to increase spacing between columns. Tried what Bruno suggested here. Its not working. I have also tried using a solution from here with no luck. Ref. image below.
Also if i cell event to default is not working.
e.g.
table.getDefaultCell().setCellEvent()
Any suggestions ?
Update:
My Output
I was able to get the padding as i want by parsing the HTML following way:
public PdfPTable getTable(String cleanHTML) throws IOException
{
String CSS = "tr { text-align: center; } td { padding: 5px; }";
CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(new ByteArrayInputStream(cleanHTML.getBytes()));
return (PdfPTable) elements.get(0);
}
That fixes the issue mentioned in question 2. Q3 is not longer required.

PDFBox: put two A4 pages on one A3

I have a pdf document with one or more pages A4 paper.
The resulting pdf document should be A3 paper where each page contains two from the first one (odd on the left, even on the right side).
I already got it to render the A4 pages into images and the odd pages are successfully placed on the first parts of a new A3 pages but I cannot get the even pages to be placed.
public class CreateLandscapePDF {
public void renderPDF(File inputFile, String output) {
PDDocument docIn = null;
PDDocument docOut = null;
float width = 0;
float height = 0;
float posX = 0;
float posY = 0;
try {
docIn = PDDocument.load(inputFile);
PDFRenderer pdfRenderer = new PDFRenderer(docIn);
docOut = new PDDocument();
int pageCounter = 0;
for(PDPage pageIn : docIn.getPages()) {
pageIn.setRotation(270);
BufferedImage bufferedImage = pdfRenderer.renderImage(pageCounter);
width = bufferedImage.getHeight();
height = bufferedImage.getWidth();
PDPage pageOut = new PDPage(PDRectangle.A3);
PDImageXObject image = LosslessFactory.createFromImage(docOut, bufferedImage);
PDPageContentStream contentStream = new PDPageContentStream(docOut, pageOut, AppendMode.APPEND, true, true);
if((pageCounter & 1) == 0) {
pageOut.setRotation(90);
docOut.addPage(pageOut);
posX = 0;
posY = 0;
} else {
posX = 0;
posY = width;
}
contentStream.drawImage(image, posX, posY);
contentStream.close();
bufferedImage.flush();
pageCounter++;
}
docOut.save(output + "\\LandscapeTest.pdf");
docOut.close();
docIn.close();
} catch(IOException io) {
io.printStackTrace();
}
}
}
I'm using Apache PDFBox 2.0.2 (pdfbox-app-2.0.2.jar)
Thank you very much for your help and the link to the other question - I think I already read it but wasn't able to use in in my code yet.
But finally the PDFClown made the job, though I think it's not very nice to use PDFBox and PDFClown in the same program.
Anyway here's my working code to combine A4 pages on A3 paper.
public class CombinePages {
public void run(String input, String output) {
try {
Document source = new File(input).getDocument();
Pages sourcePages = source.getPages();
Document target = new File().getDocument();
Page targetPage = null;
int pageCounter = 0;
double moveByX = .0;
for(Page sourcePage : source.getPages()) {
if((pageCounter & 1) == 0) {
//even page gets a blank page
targetPage = new Page(target);
target.setPageSize(PageFormat.getSize(PageFormat.SizeEnum.A3, PageFormat.OrientationEnum.Landscape));
target.getPages().add(targetPage);
moveByX = .0;
} else {
moveByX = .50;
}
//get content from source page
XObject xObject = sourcePages.get(pageCounter).toXObject(target);
PrimitiveComposer composer = new PrimitiveComposer(targetPage);
Dimension2D targetSize = targetPage.getSize();
Dimension2D sourceSize = xObject.getSize();
composer.showXObject(xObject, new Point2D.Double(targetSize.getWidth() * moveByX, targetSize.getHeight() * .0), new Dimension(sourceSize.getWidth(), sourceSize.getHeight()), XAlignmentEnum.Left, YAlignmentEnum.Top, 0);
composer.flush();
pageCounter++;
}
target.getFile().save(output + "\\CombinePages.pdf", SerializationModeEnum.Standard);
source.getFile().close();
} catch (FileNotFoundException fnf) {
log.error(fnf);
} catch (IOException io) {
log.error(io);
}
}
}

pdfbox generate multi page

When I generate second page, I get a blank page. I have two functions. One generates a table with text, and another generates a PDF. When end place in first page, I add another page and I want write in new page. When I open generated PDF file, the second page is blank:
//global variables
PDPage nowa=null;
PDPageContentStream contentStream1 = null;
//function to generate table
private void print_sumActionPerformed(java.awt.event.ActionEvent evt){
try {
PDDocument doc = null;
PDPage page = null;
int max_row=55;
int suma=0;
int pozycja=0;
final int starty=760;
final int startx=30;
try{
doc = new PDDocument();
page = new PDPage(PDPage.PAGE_SIZE_A4);
doc.addPage(page);
PDFont font = PDType1Font.HELVETICA;
PDPageContentStream content = new PDPageContentStream(doc, page,true,true);
//some code to generate table
drawTable(page, content, starty, startx, content1,doc);
content.close();
doc.save("path");
doc.close();
Thread.sleep(500);
} catch (Exception e){
System.out.println(e);
}
} catch (IOException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
} catch (PrinterException ex) {
Logger.getLogger(Okno.class.getName()).log(Level.SEVERE, null, ex);
}
}
function to generate table in pdf
private void drawTable(PDPage page, PDPageContentStream contentStream,float y, float margin, String[][] content,PDDocument doc) throws IOException, InterruptedException {
final int rows = content.length;
final int cols = content[0].length;
final float rowHeight = 13f;
final float marginCell=5;
final float startx=30;
final int tableWidth3=200;
final int tableWidth5=250;
boolean new_kol;
if (margin<230){
new_kol=false;
}else {
new_kol=true;
}
float textx;
final float odst=20;
final float starty = y;
float texty=starty-rowHeight+3;
//width table
int tableWidth;
if(cols==5)tableWidth=tableWidth5;
else tableWidth=tableWidth3;
//start print table in pdf
if(!new_kol){
contentStream.drawLine(startx,starty,tableWidth+startx+marginCell,starty);
textx=startx+marginCell;
}else {
contentStream.drawLine(startx+tableWidth5+odst, starty, 2*tableWidth5+(startx)+odst+marginCell,starty);
textx=startx+marginCell+tableWidth+odst;
}
for(int i = 0; i < content.length; i++){
for(int j = 0 ; j < content[i].length; j++){
//linia pionowa
contentStream.drawLine(textx-marginCell,texty-3,textx-marginCell,texty-3+rowHeight);
String text=content[i][j];
if(text.contains("AB")){
contentStream.setFont(PDType1Font.HELVETICA_BOLD,10);
}else contentStream.setFont(PDType1Font.HELVETICA,8);
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx,texty);
contentStream.drawString(text);
contentStream.endText();
switch(j){
case 0: textx+=30;
break;
case 1:
if (cols==5) textx += 120;
else textx+=150;
break;
case 2: textx+=15;
if(cols==3 ) contentStream.drawLine(textx+marginCell,texty-3,textx+marginCell,texty-3+rowHeight);
break;
case 3: textx+=40;
break;
case 4: textx+=40;
contentStream.drawLine(textx+marginCell,texty-3,textx+marginCell,texty-3+rowHeight);
break;
}
}
if(new_kol){
textx=tableWidth+startx+odst+marginCell;
contentStream.drawLine(textx-marginCell,texty-3,textx+tableWidth,texty-3);
}
else {
textx=startx+marginCell;
contentStream.drawLine(textx-marginCell,texty-3,textx+tableWidth,texty-3);
}
if((texty-=rowHeight)<50){
if(!new_kol){
new_kol=true;
contentStream.drawLine(startx,texty+10,startx+tableWidth+marginCell,texty+10);
texty=760-10;
textx=tableWidth+startx+odst+marginCell;
contentStream.drawLine(textx-marginCell,texty+10,textx+tableWidth,texty+10);
}else{
new_kol=false;
contentStream.drawLine(startx+tableWidth5+odst,texty+10,startx+2*tableWidth,texty+10);
texty=760-10;
textx=startx+marginCell;
//here i add new page when end page height
//i get blank page
contentStream.close();
page=null;
contentStream=null;
nowa=new PDPage(PDPage.PAGE_SIZE_A4);
page=null;
page=nowa;
doc.addPage(nowa);
PDFont font = PDType1Font.HELVETICA;
contentStream1 = new PDPageContentStream(doc, nowa,false,true);
contentStream=contentStream1;
contentStream1=null;
contentStream.drawLine(textx,texty+10,textx+tableWidth,texty+10);
}
}
}
}
It could probably be your constructor. I think if you use :
PDPageContentStream content = new PDPageContentStream(doc, page,true,true, false);
it should work as expected. The final parameter corresponds to the resetContext value in the constructor and decides whether the current graphic context should be reset or not (Source) .

Categories