Skip to content

Commit

Permalink
Fix LibrePDF/OpenPDF5#7: LineSeparator prints out OBJECT_REPLACEMENT_…
Browse files Browse the repository at this point in the history
…CHARACTER
  • Loading branch information
Tonny-Gu committed May 22, 2021
1 parent 4a36fdf commit ae19441
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
13 changes: 13 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,19 @@ boolean isHorizontalSeparator() {
}
return false;
}

/**
* Checks if this <CODE>PdfChunk</CODE> is a vertical Separator Chunk.
* @return true if this chunk is a vertical separator.
* @since OpenPDF
*/
boolean isVerticalSeparator() {
if (isAttribute(Chunk.SEPARATOR)) {
Object[] o = (Object[])getAttribute(Chunk.SEPARATOR);
return (Boolean) o[1];
}
return false;
}

/**
* Checks if this <CODE>PdfChunk</CODE> is a tab Chunk.
Expand Down
3 changes: 3 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,9 @@ else if (isJustified) {
if (chunk.isImage()) {
adjustMatrix = true;
}
else if (chunk.isVerticalSeparator()) {
// Did nothing here to avoid printing out OBJECT_REPLACEMENT_CHARACTER
}
else if (chunk.isHorizontalSeparator()) {
PdfTextArray array = new PdfTextArray();
array.add(-glueWidth * 1000f / chunk.font.size() / hScale);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Author: alesky78 <alessandro.dottavio@gmail.com>
*/

package com.lowagie.text.pdf;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.junit.jupiter.api.Test;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.draw.LineSeparator;

public class ColumnTextSeparator {

private String filePath;

public static final float[][] COLUMNS = {{ 36, 36, 296, 806 } , { 299, 36, 559, 806 }};

@Test
public void test_columnTextSeparator() throws Exception{
filePath = System.getProperty("user.dir")+"/src/test/resources";

File RESULT = new File(filePath+"/columnTextSeparator.pdf");
// step 1
Document document = new Document(PageSize.A4);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);

document.open();
PdfContentByte wrote = pdfWriter.getDirectContent();

ColumnText ct = new ColumnText(wrote);
Phrase p = null;

for (int i = 0; i < 3; i++) {
p = new Phrase();
p.add(new LineSeparator(0.3f, 100, null, Element.ALIGN_CENTER, -2));
p.add("test");
ct.addText(p);
ct.addText(Chunk.NEWLINE);
}

ct.setAlignment(Element.ALIGN_JUSTIFIED);
ct.setExtraParagraphSpace(6);
ct.setLeading(0, 1.2f);
ct.setFollowingIndent(27);
int linesWritten = 0;
int column = 0;
int status = ColumnText.START_COLUMN;
while (ColumnText.hasMoreText(status)) {
ct.setSimpleColumn(COLUMNS[column][0], COLUMNS[column][1],COLUMNS[column][2], COLUMNS[column][3]);
ct.setYLine(COLUMNS[column][3]);
status = ct.go();
linesWritten += ct.getLinesWritten();
column = Math.abs(column - 1);
if (column == 0)
document.newPage();
}

document.close();

FileOutputStream fos = new FileOutputStream(RESULT);
fos.write(baos.toByteArray());
fos.close();
}






}

0 comments on commit ae19441

Please sign in to comment.