当前位置: 首页 > 知识库问答 >
问题:

Java-apache PDFBox两个A3纸到一个A2?

阚小云
2023-03-14

我有一台不支持我需要的功能的打印机。

打印机打印A2纸张大小。我想打印两张A3大小的纸,可以放在一张A2大小的纸上,但我的打印机不支持这一点。

我已经打电话给公司的技术支持部门,但他们告诉我需要买一台新的,因为我的打印机不支持这个功能。(这很有趣,因为更旧版本的打印机确实支持此功能)。

所以我尝试使用Apache PDFBox,在那里我可以像这样加载我的pdf文件:

File pdfFile = new File(path);
PDDocument pdfDocument = load(pdfFile);

我加载的文件是sizeA3。我认为如果我可以获得A2纸张大小的新PDDocument就足够了。然后将我加载的pdfFile两次放入A2纸张中。

总之,我需要在一个页面上加载两次的文件。我只是不知道该怎么做。

最好的问候。

共有1个答案

呼延升
2023-03-14

您可能需要查看PageCombinationSample。java根据其JavaDoc几乎可以满足您的需要:

此示例演示如何使用表单XObjects[PDF:1.6:4.9]将多个页面合并为一个更大的页面(例如,将两个A4模块合并为一个A3模块)。

表单XObject是一种方便的方法,可以在多个页面上多次将内容表示为模板。

中央代码:

// 1. Opening the source PDF file...
File sourceFile = new File(filePath);

// 2. Instantiate the target PDF file!
File file = new File();

// 3. Source page combination into target file.
Document document = file.getDocument();
Pages pages = document.getPages();
int pageIndex = -1;
PrimitiveComposer composer = null;
Dimension2D targetPageSize = PageFormat.getSize(SizeEnum.A4);
for(Page sourcePage : sourceFile.getDocument().getPages())
{
    pageIndex++;
    int pageMod = pageIndex % 2;
    if(pageMod == 0)
    {
        if(composer != null)
        {composer.flush();}

        // Add a page to the target document!
        Page page = new Page(
            document,
            PageFormat.getSize(SizeEnum.A3, OrientationEnum.Landscape)
        ); // Instantiates the page inside the document context.
        pages.add(page); // Puts the page in the pages collection.
        // Create a composer for the target content stream!
        composer = new PrimitiveComposer(page);
    }

    // Add the form to the target page!
    composer.showXObject(
        sourcePage.toXObject(document), // Converts the source page into a form inside the target document.
        new Point2D.Double(targetPageSize.getWidth() * pageMod, 0),
        targetPageSize,
        XAlignmentEnum.Left,
        YAlignmentEnum.Top,
        0
    );
}
composer.flush();

// 4. Serialize the PDF file!
serialize(file, "Page combination", "combining multiple pages into single bigger ones", "page combination");

// 5. Closing the PDF file...
sourceFile.close();
 类似资料:
  • 我正在用ApachePDFBox编写一个简单的Java应用程序。我有几个PDF,其中最后一页是前几页内容的索引。 我需要的索引(最后一页)成为PDF文件的第一页。 可能吗? 我还发现了http://itextpdf.com/这个库听起来比ApachePDFBox更好,但在这种情况下,我也不知道我是否能做我需要的事情 或者我可以用这个:http://saaspose.com/docs/display

  • 我在WebDriver中的测试有问题。 在第一个包我有2类(页)主页,登录页。 在secound包中,我有test-goToLiginPageTest和LoginTest。 在Gotoligini页面,我检查,我在主页上,并进入登录页面。 在LoginTest中,我检查我是否在登录页面上,并登录。 但来自goToLiginPageTest的两个测试通过,但来自LoginTest的测试失败。 我不确

  • 问题内容: 我需要将两个表映射到一个类,但很难弄清楚。一个表是ROOMS,另一个表是TRAINERS。 ROOMS表: 培训人员表: 表应当加入了,和。 ROOMS表的主键是:。TRAINERS表的主键是:。 我需要通过查询这个数据,,,,和。 在SQL中,可能类似于: 我正在项目中其他地方使用ROOMS表,它已经映射为独立对象。是否有一种方法可以将其用作TRAINERS对象的子对象,或者将这两个

  • 我需要映射两个GET方法,如下所示: 但当我绘制这张地图时,Spring被搞糊涂了! 当我击中http://localhost:8080/tickets在Chrome中,服务器上的结果是: 当我击中http://localhost:8080/tickets/12在Chrome中,服务器上的结果是: 我的Spring控制器是: 我的单元测试是: 我正在使用Java11和SpringBoot2.1.6

  • 在我的页面中,我有两个AMP表单,一个提交按钮放在第一个表单中,另一个div提交成功按钮放在第二个表单中。我如何在第二个表格中显示第一个表格提交的状态?

  • 在一个表中有两列可以是来自同一表的外键,这样可以吗? 例如: 我有一个“address”表,表中有列: id,street,city_id,state,country(或id,street,city_id,state_id,state_id) 我有一个'states'表,保存状态和国家:id、name、level 那么我可以把states_id放在地址表的state和country中吗(如上面的括