Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Itext文档以匹配图像大小_Java_Image_Itext - Fatal编程技术网

Java Itext文档以匹配图像大小

Java Itext文档以匹配图像大小,java,image,itext,Java,Image,Itext,我正在尝试使用带有A4页面属性的iText将图像添加到pdf: com.itextpdf.text.Document document = new com.itextpdf.text.Document( com.itextpdf.text.PageSize.A4); PdfWriter.getInstance(document, new FileOutputStream(m_pathToCreateFileIn + "my_web.pdf"));

我正在尝试使用带有
A4
页面属性的iText将图像添加到pdf:

com.itextpdf.text.Document document = new com.itextpdf.text.Document(
                com.itextpdf.text.PageSize.A4);
        PdfWriter.getInstance(document, new FileOutputStream(m_pathToCreateFileIn + "my_web.pdf"));
        System.out.println("New pdf -> " + m_pathToCreateFileIn + "my_web.pdf");
        document.open();
        Image image = Image.getInstance(pngPath);
        image.scaleToFit(com.itextpdf.text.PageSize.A4.getWidth(), com.itextpdf.text.PageSize.A4.getHeight());
        document.add(image);
我将
文档
图像
都设置为A4页面大小,但图像仍然不适合我的文档页面大小


谢谢您的帮助。

尝试使用此方法按x轴和y轴调整参数

  String imageUrl1 = "";
  Image image1 = Image.getInstance(new URL(imageUrl1));
  image1.scaleAbsolute(140, 190);
  image1.setAbsolutePosition(450, 580);
  document.add(image1);

尝试使用此方法通过x轴和y轴调整参数

  String imageUrl1 = "";
  Image image1 = Image.getInstance(new URL(imageUrl1));
  image1.scaleAbsolute(140, 190);
  image1.setAbsolutePosition(450, 580);
  document.add(image1);

在更改生效之前,必须添加document.newPage(),因为无法更改当前页面。尝试:

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(m_pathToCreateFileIn + "my_web.pdf"));
    document.open();
    Image image = Image.getInstance(pngPath);
    image.scaleToFit(PageSize.A4);
    document.setPageSize(PageSize.A4);
    document.newPage();
    document.add(image);
    document.close();

在更改生效之前,必须添加document.newPage(),因为无法更改当前页面。尝试:

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(m_pathToCreateFileIn + "my_web.pdf"));
    document.open();
    Image image = Image.getInstance(pngPath);
    image.scaleToFit(PageSize.A4);
    document.setPageSize(PageSize.A4);
    document.newPage();
    document.add(image);
    document.close();

我知道派对已经太迟了,但我想在@DonKanallie的回答上再扩展一点(顺便说一句,这非常有用),以便在处理不同大小/尺寸的图像时更灵活一点。正如最初的海报所提到的,我的图像不适合A4输出,因此需要考虑图像的大小和下面的自定义矩形

        Document document = new Document();
        FileOutputStream fos = new FileOutputStream(outputPath);            
        Image image = Image.getInstance(inputFile);
        //Get Size of Original Image for conversion
        float origWidth = image.getWidth();
        float origHeight = image.getHeight();
        image.scaleToFit(origWidth,origHeight);
        //Set position of image in top left corner
        image.setAbsolutePosition(0,0);
        //Create Rectangle in support of new page size
        Rectangle rectangle = new Rectangle(origWidth,origHeight);
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        writer.open();
        document.open();
        //Set page size before adding new page
        document.setPageSize(rectangle);
        document.newPage();
        document.add(image);
        document.close();
        writer.close();

我知道派对已经太迟了,但我想在@DonKanallie的回答上再扩展一点(顺便说一句,这非常有用),以便在处理不同大小/尺寸的图像时更灵活一点。正如最初的海报所提到的,我的图像不适合A4输出,因此需要考虑图像的大小和下面的自定义矩形

        Document document = new Document();
        FileOutputStream fos = new FileOutputStream(outputPath);            
        Image image = Image.getInstance(inputFile);
        //Get Size of Original Image for conversion
        float origWidth = image.getWidth();
        float origHeight = image.getHeight();
        image.scaleToFit(origWidth,origHeight);
        //Set position of image in top left corner
        image.setAbsolutePosition(0,0);
        //Create Rectangle in support of new page size
        Rectangle rectangle = new Rectangle(origWidth,origHeight);
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        writer.open();
        document.open();
        //Set page size before adding new page
        document.setPageSize(rectangle);
        document.newPage();
        document.add(image);
        document.close();
        writer.close();

这是正常的,如果图像没有相同的纵横比作为一个A4页。假设你把人的照片放在A4页上,你肯定不想看到一组人,根据他们照片的长宽比,他们的头很胖或很瘦?因为如果您使用
scaleAbsolute()
而不是
scaleToFit()
,就会发生这种情况。现在它会更清晰,谢谢。无论如何,我认为最好定义页面大小,使其与图像大小相同……如果图像的纵横比与A4页面不同,这是正常的。假设你把人的照片放在A4页上,你肯定不想看到一组人,根据他们照片的长宽比,他们的头很胖或很瘦?因为如果您使用
scaleAbsolute()
而不是
scaleToFit()
,就会发生这种情况。现在它会更清晰,谢谢。无论如何,我认为最好定义页面大小,使其与图像大小相同……问题是将图像与A4页面匹配。A4纸的尺寸不是140乘190个用户单位。此外,如果A4页是从头开始创建的,则偏移量将是
(0,0)
,而不是
(450580)
。这是一个非常懒惰的答案,因此投了反对票。问题是将图片与A4页面匹配。A4纸的尺寸不是140乘190个用户单位。此外,如果A4页是从头开始创建的,则偏移量将是
(0,0)
,而不是
(450580)
。这是一个非常懒散的回答,因此投了反对票。