You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Andreas Lehmkühler (JIRA)" <ji...@apache.org> on 2014/12/23 13:52:13 UTC
[jira] [Closed] (PDFBOX-2581) Text corruption
[ https://issues.apache.org/jira/browse/PDFBOX-2581?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Andreas Lehmkühler closed PDFBOX-2581.
--------------------------------------
Resolution: Invalid
Assignee: Andreas Lehmkühler
Please don't use JIRA for Q&A purposes. Use our [mailing lists|http://pdfbox.apache.org/mailinglists.html] instead.
You have to reset the context of the existing page before adding new content. To do so use the following constructor:
{code}
PDPageContentStream content = new PDPageContentStream(document, pdfPage, true, true, true);
{code}
Have a look at the [AddMessageToEachPage|http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddMessageToEachPage.java?view=markup] example for further information.
> Text corruption
> ---------------
>
> Key: PDFBOX-2581
> URL: https://issues.apache.org/jira/browse/PDFBOX-2581
> Project: PDFBox
> Issue Type: Bug
> Affects Versions: 1.8.8
> Reporter: pupkin
> Assignee: Andreas Lehmkühler
>
> I use PDFBox 1.8.8 and try to add new text to existing pdf file. The problem is that text added by me is looking compressed for this specific pdf
>
> [original pdf (i think this file is source of problem)|https://www.dropbox.com/s/foeb9raub20lbnx/1.pdf?dl=0]
> (it is working for other pdfs).
> [pdfbox generated file|https://www.dropbox.com/s/4e5ozksm5ycv7v2/2.pdf?dl=0]
>
>
> [image of problem with explanation|https://www.dropbox.com/s/8r7hwq4d1i5yuxz/2014-12-23-11-04-14-5937500.png?dl=0]
>
> It was not shown in all originally but i fixed problem with this command (set font display mode)
> {code}
> content.appendRawCommands("0 Tr ");
> {code}
> I was working with pdf matrix and checked almost everything ... and do not see way how may i fix the problem...
> {code}
> content.setTextMatrix(1, 0, 0, 1, 0, 0);
> {code}
> questions:
> 1) why pdfbox not ignore page font settings and use default options of font on page?
> 2) what mode was used in my pdf to archive such kind transformation how i may fix problem to show font correctly?
> {code}
> private static void writePdfBoxStamp(PDDocument document, int page,
> float topLeftX, float topLeftY, float width, float height,
> String text, float itemFontSize, int[] color, int rotation) throws IOException {
> PDPage pdfPage = (PDPage) document.getDocumentCatalog().getAllPages().get(page);
> PDPageContentStream content = new PDPageContentStream(document, pdfPage, true, true);
> // COSDictionary pageFonts = pdfPage.getResources().getCOSDictionary();
> //border draw
> float lineWidth = 2;
> float bottomLeftX = topLeftX;
> float bottomLeftY = topLeftY - height;
> float topRightX = topLeftX + width;
> float topRightY = topLeftY;
> float radius = 10;
> content.setLineWidth(lineWidth);
> content.setStrokingColor(new Color(color[0], color[1], color[2]));
> content.setNonStrokingColor(new Color(color[0], color[1], color[2]));
> PDFont font = PDType1Font.HELVETICA_BOLD;
> int fontSize = (int) itemFontSize;
> float linesHeight = font.getFontDescriptor().getFontBoundingBox()
> .getHeight()
> / 1000 * fontSize;
> float txtLineWidth = font.getStringWidth(text)
> / 1000 * fontSize;
> content.setFont(font, fontSize);
> content.beginText();
> content.setFont(font, fontSize);
> float posTextX = topLeftX + (width-txtLineWidth)/2;
> float posTextY = topLeftY - height/2 - linesHeight/2 + lineWidth;
> if (rotation > 0) {
> int step = 0;
> // clockwise rotation
> if (rotation == 90) {
> step = 6;
> posTextX = topLeftX + (width + linesHeight)/2 - lineWidth;
> posTextY = topLeftY - (height + txtLineWidth)/2;
> }
> if (rotation == 270) {
> step = -6;
> posTextX = topLeftX + (width - linesHeight/2)/2;
> posTextY = topLeftY - (height - txtLineWidth)/2;
> }
> content.setTextRotation(-step*Math.PI*0.25, posTextX, posTextY);
> } else {
> content.moveTextPositionByAmount(posTextX,posTextY);
> }
> content.appendRawCommands("0 Tr "); //set normal text procession
> content.drawString(text);
> content.endText();
> content.stroke();
> content.close();
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)