[ 
https://issues.apache.org/jira/browse/PDFBOX-5898?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17900584#comment-17900584
 ] 

Tilman Hausherr edited comment on PDFBOX-5898 at 11/23/24 2:47 PM:
-------------------------------------------------------------------

I have been able to reproduce the effect:
{code:java}
public class PDFBOX5898
{
    public static void main(String[] args) throws IOException
    {
        int factor = 50; // effect should appear at 10; appears also at 15, 25, 
50; not at 20 or 100
        float step = 0.75f;
        BufferedImage bim = new BufferedImage((int) (8 * factor * step), (int) 
(8 * factor * step), BufferedImage.TYPE_INT_ARGB); // happens also with RGB
        Graphics2D g = bim.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON); // VALUE_ANTIALIAS_ON one does it
        g.setColor(new Color(0.35686f,0.60784f,0.83529f));
        g.transform(AffineTransform.getScaleInstance(factor, factor));
        for (float x = 0; x < bim.getWidth(); x += step)
        {
            for (float y = 0; y < bim.getHeight(); y += step)
            {
                GeneralPath linePath = new GeneralPath();
                linePath.moveTo(x, y);
                linePath.lineTo(x + 0.75f, y);
                linePath.lineTo(x + 0.75f, y + 0.75f);
                linePath.lineTo(x, y + 0.75f);
                linePath.closePath();
                g.fill(linePath);
            }
        }
        g.dispose();
        ImageIO.write(bim, "png", new File("bim.png"));
    }
}
{code}
So it's the anti aliasing that does it. {{Pagedrawer.java}} has this:
{code:java}
        Rectangle2D bounds = linePath.getBounds2D();
        boolean noAntiAlias = isRectangular(linePath) && bounds.getWidth() > 1 
&&
                                                         bounds.getHeight() > 1;
        if (noAntiAlias)
        {
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                      RenderingHints.VALUE_ANTIALIAS_OFF);
        }
{code}
I've tried activating that part for bounds larger than 1x1 and got many 
differences where the results looked worse, e.g. PDFBOX-1658.


was (Author: tilman):
I have been able to reproduce the effect:
{code:java}
public class PDFBOX5898
{
    public static void main(String[] args) throws IOException
    {
        int factor = 50; // effect should appear at 10; appears also at 15, 25, 
50; not at 20 or 100
        float step = 0.75f;
        BufferedImage bim = new BufferedImage((int) (8 * factor * step), (int) 
(8 * factor * step), BufferedImage.TYPE_INT_ARGB); // happens also with RGB
        Graphics2D g = bim.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON); // VALUE_ANTIALIAS_ON one does it
        g.setColor(new Color(0.35686f,0.60784f,0.83529f));
        g.transform(AffineTransform.getScaleInstance(factor, factor));
        for (float x = 0; x < bim.getWidth(); x += step)
        {
            for (float y = 0; y < bim.getHeight(); y += step)
            {
                GeneralPath linePath = new GeneralPath();
                linePath.moveTo(x, y);
                linePath.lineTo(x + 0.75f, y);
                linePath.lineTo(x + 0.75f, y + 0.75f);
                linePath.lineTo(x, y + 0.75f);
                linePath.closePath();
                System.out.println(linePath.getBounds2D());
                g.fill(linePath);
            }
        }
        g.dispose();
        ImageIO.write(bim, "png", new File("bim.png"));
    }
}
{code}
So it's the anti aliasing that does it. {{Pagedrawer.java}} has this:
{code:java}
        Rectangle2D bounds = linePath.getBounds2D();
        boolean noAntiAlias = isRectangular(linePath) && bounds.getWidth() > 1 
&&
                                                         bounds.getHeight() > 1;
        if (noAntiAlias)
        {
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                      RenderingHints.VALUE_ANTIALIAS_OFF);
        }
{code}
I've tried activating that part for bounds larger than 1x1 and got many 
differences where the results looked worse, e.g. PDFBOX-1658.

> Converting PDF to images using PDFBox results in unexpected outcomes.
> ---------------------------------------------------------------------
>
>                 Key: PDFBOX-5898
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5898
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.24
>            Reporter: bai yuan
>            Priority: Major
>         Attachments: TilingPattern-2371731399394458-1000%.png, 
> TilingPattern-2371731399416644-2000%.png, image-2024-11-12-15-45-48-011.png, 
> test2.pdf
>
>
> Running the following code, many fine red lines appear in the image.
> {code:java}
>         String pdfFilePath = path + "test2.pdf";
>         String outputDir = path + "/out/";
>         try {
>             PDDocument document = PDDocument.load(new File(pdfFilePath));
>             PDFRenderer pdfRenderer = new PDFRenderer(document);
>             for (int page = 0; page < document.getNumberOfPages(); page++) {
>                 BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, 
> ImageType.RGB);
>                 ImageIO.write(bim, "PNG", new File(outputDir + "page-" + 
> (page + 1) + ".png"));
>             }
>             document.close();
>             System.out.println("PDF converted to images successfully.");
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
> {code}
>  !image-2024-11-12-15-45-48-011.png! 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: dev-h...@pdfbox.apache.org

Reply via email to