Apache PDFBox的PDPageContentStream的showText和drawImage有时不起作用或上下颠倒显示。

huangapple go评论86阅读模式
英文:

Apache PDFBox PDPageContentStream showText and drawImage sometimes don't work or show upside down

问题

private byte[] setPageNumbers(byte[] pdftmp, Integer initialPage, Integer finalPage) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PDDocument document = PDDocument.load(pdftmp);
    PDPage page;
    String numberingFormat = "  {0}";
    int offset_X = 45;
    int offset_Y = 803;
    int page_counter = 1;
    Color color = new Color(0);
    float[] components = new float[] {color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f };
    float x;
    float y;
    PDPageContentStream contentStream;
    PDRectangle pageSize;
    String text;
    PDImageXObject pdImage;
    for(int i = initialPage; i <= finalPage; i++) {
        page = document.getPage(i);
        pageSize = page.getMediaBox();
        x = pageSize.getLowerLeftX();
        y = pageSize.getLowerLeftY();
        text = MessageFormat.format(numberingFormat,page_counter);
        contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
        contentStream.setFont(PDType1Font.HELVETICA_BOLD, 11);
        contentStream.setNonStrokingColor(new PDColor(components, PDDeviceRGB.INSTANCE));
        contentStream.beginText();
        contentStream.newLineAtOffset(x + pageSize.getWidth() - offset_X, y+offset_Y);
        contentStream.showText(text);
        contentStream.endText();
        ++page_counter;        
        pdImage = PDImageXObject.createFromFile(PDChoice.class.getResource("/images/logo.png").getPath(),document);
        contentStream.drawImage(pdImage, x + pageSize.getWidth() - offset_X - 7, y + offset_Y - 2);
        contentStream.close();                    
    }
    document.save(baos);
    document.close();
    return baos.toByteArray();
}
英文:

Regards,
I have a function that put page numbers and logos at right top.
The code works fine in almost every pages.
But, in some pages, the page number and its logo don't show.
In another pages, the page number and its logo show upside down at the left bottom.

Any idea about causes?

Code bellow, thanks.

    private byte[] setPageNumbers(byte[] pdftmp, Integer initialPage, Integer finalPage) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDDocument document = PDDocument.load(pdftmp);
PDPage page;
String numberingFormat = &quot;  {0}&quot;;
int offset_X = 45;
int offset_Y = 803;
int page_counter = 1;
Color color = new Color(0);
float[] components = new float[] {color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f };
float x;
float y;
PDPageContentStream contentStream;
PDRectangle pageSize;
String text;
PDImageXObject pdImage;
for(int i = initialPage; i &lt;= finalPage; i++) {
page = document.getPage(i);
pageSize = page.getMediaBox();
x = pageSize.getLowerLeftX();
y = pageSize.getLowerLeftY();
text = MessageFormat.format(numberingFormat,page_counter);
contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 11);
contentStream.setNonStrokingColor(new PDColor(components, PDDeviceRGB.INSTANCE));
contentStream.beginText();
contentStream.newLineAtOffset(x + pageSize.getWidth() - offset_X, y+offset_Y);
contentStream.showText(text);
contentStream.endText();
++page_counter;		
pdImage = PDImageXObject.createFromFile(PDChoice.class.getResource(&quot;/images/logo.png&quot;).getPath(),document);
contentStream.drawImage(pdImage, x + pageSize.getWidth() - offset_X - 7, y + offset_Y - 2);
contentStream.close();					
}
document.save(baos);
document.close();
return baos.toByteArray();
}

答案1

得分: 2

我是这样解决的...

...
int degree;
float tx;
float ty;
width = Math.round(pageSize.getWidth()*25.4/72); // pts to mm
height = Math.round(pageSize.getHeight()*25.4/72); // pts to mm
degree = page.getRotation();
// 我想要A4纵向...
// 它是横向的... 转为纵向
if (degree == 90 || degree == 270 || width > height) {
page.setRotation(0);
}
// 它不是A4尺寸... 调整为
if (width != 210.0 || height != 297.0) {
page.setMediaBox(PDRectangle.A4);
}
...
// 有些页面是正确的,但需要旋转。我可能不想旋转这些页面,所以
// 我旋转内容
if (degree == 180) {
tx = (pageSize.getLowerLeftX() + pageSize.getUpperRightX()) / 2;
ty = (pageSize.getLowerLeftY() + pageSize.getUpperRightY()) / 2;
contentStream.transform(Matrix.getTranslateInstance(tx, ty));
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(180), 0, 0));
contentStream.transform(Matrix.getTranslateInstance(-tx, -ty));
}
...
英文:

I solved this way...

...
int degree;
float tx;
float ty;
width = Math.round(pageSize.getWidth()*25.4/72); // pts to mm
height = Math.round(pageSize.getHeight()*25.4/72); // pts to mm
degree = page.getRotation();
// I want A4 portrait...
// It&#39;s landscape... change to portrait
if (degree == 90 || degree == 270 || width &gt; height) {
page.setRotation(0);
}
// It is not A4... change to
if (width != 210.0 || height != 297.0) {
page.setMediaBox(PDRectangle.A4);
}
...
// Some pages are right, but with rotation. I may not rotate this pages, so
// I rotate the content
if (degree == 180) {
tx = (pageSize.getLowerLeftX() + pageSize.getUpperRightX()) / 2;
ty = (pageSize.getLowerLeftY() + pageSize.getUpperRightY()) / 2;
contentStream.transform(Matrix.getTranslateInstance(tx, ty));
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(180), 0, 0));
contentStream.transform(Matrix.getTranslateInstance(-tx, -ty));
}
...

huangapple
  • 本文由 发表于 2020年8月27日 01:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定