使用PDFBox旋转几种形状的问题

问题描述 投票:0回答:1

我做了一个小的Java工具,可以根据输入数据生成PDF蓝图,我使用PDFBox库进行生成。

当我在程序中添加矩形时,我想旋转它。对于一个矩形,效果很好,但是当我有几个矩形时,该程序无法正常工作。

为了说明我的问题,这是我想做的:

First screen

这就是我的程序所提供的:

Second screen

显然,我对矩阵有问题。谁能指导我?

否则,有人知道用于生成蓝图的特定Java库吗?

谢谢您的帮助,

这里的代码:

public static void main(String[] args) {
    PDDocument doc = new PDDocument();

    try {
        PDPage page = new PDPage();
        doc.addPage(page);

        PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true);

        float[] fTx = { 100, 100, 500, 340 };
        float[] fTy = { 500, 700, 700, 300 };
        Color[] cArray = { Color.BLUE, Color.RED, Color.ORANGE, Color.MAGENTA };

        int[] tAngles = { 0, -90, 180, 90 }; /* in degree */
        int angle;
        int previousAngle = 0;

        /*  */
        contents.addRect(100, 500, 400, 200);
        contents.stroke();

        for (int i = 1; i < 4; i++) {

            /* transform */
            contents.transform(Matrix.getTranslateInstance(fTx[i - 1], fTy[i - 1]));
            contents.transform(Matrix.getRotateInstance(Math.toRadians(tAngles[i - 1] - previousAngle), 0, 0));
            previousAngle = tAngles[i - 1];
            contents.transform(Matrix.getTranslateInstance(-fTx[i - 1], -fTy[i - 1]));

            /* Shapes */
            contents.setNonStrokingColor(cArray[i - 1]);
            contents.addRect(fTx[i - 1], fTy[i - 1], 100, 20);
            contents.fillAndStroke();

            contents.setNonStrokingColor(Color.GREEN);
            contents.addRect(fTx[i - 1], fTy[i - 1], 5, 5);
            contents.fillAndStroke();

        }

        /* label */
        contents.setNonStrokingColor(Color.BLACK);
        contents.beginText();
        contents.newLineAtOffset(300, 400);
        contents.setFont(PDType1Font.HELVETICA, 10);
        contents.showText("Test label");
        contents.endText();
        contents.closeAndStroke();

        /* End */
        contents.close();

        doc.save(System.getProperty("user.home") + "/Desktop/testPDFBox.pdf");
        doc.close();

        /* Open pdf generated */
        File f = new File(System.getProperty("user.home") + "/Desktop/testPDFBox.pdf");
        Desktop.getDesktop().open(f);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

二手图书馆:

  • fontbox-2.0.16.jar
  • pdfbox-2.0.16.jar
  • pdfbox-app-2.0.16.jar
  • pdfbox-tools-2.0.16.jar
  • preflight-2.0.16.jar
  • preflight-app-2.0.16.jar
  • xmpbox-2.0.16.jar

胜利者

java pdf matrix pdfbox
1个回答
0
投票

这有效,我保存并恢复了图形状态,这更容易理解。我还调整了最后的x和y转换值,并增加了循环的上限。

PDPageContentStream contents = new PDPageContentStream(doc, page, AppendMode.APPEND, true);

//float[] fTx = { 100, 100, 500, 340 };
//float[] fTy = { 500, 700, 700, 300 };
float[] fTx = { 100, 100, 500, 500 };
float[] fTy = { 500, 700, 700, 500 };
Color[] cArray = { Color.BLUE, Color.RED, Color.ORANGE, Color.MAGENTA };

int[] tAngles = { 0, -90, 180, 90 }; /* in degree */
/* in degree */
int angle;
//int previousAngle = 0;

/*  */
contents.addRect(100, 500, 400, 200);
contents.stroke();

for (int i = 1; i <= 4; i++)
{
    contents.saveGraphicsState();
    /* transform */
    contents.transform(Matrix.getTranslateInstance(fTx[i - 1], fTy[i - 1]));
    contents.transform(Matrix.getRotateInstance(Math.toRadians(tAngles[i - 1] /* - previousAngle */), 0, 0));
    //TH not needed previousAngle = tAngles[i - 1];
    contents.transform(Matrix.getTranslateInstance(-fTx[i - 1], -fTy[i - 1]));

    /* Shapes */
    contents.setNonStrokingColor(cArray[i - 1]);
    contents.addRect(fTx[i - 1], fTy[i - 1], 100, 20);
    contents.fillAndStroke();

    contents.setNonStrokingColor(Color.GREEN);
    contents.addRect(fTx[i - 1], fTy[i - 1], 5, 5);
    contents.fillAndStroke();
    contents.restoreGraphicsState();

}

与问题无关:您不需要所有的jar文件;如果您有应用程序,则不需要单独的罐子。当前的PDFBox版本是2.0.17。

© www.soinside.com 2019 - 2024. All rights reserved.