gpt4 book ai didi

java - 写入 PDF 文件第 2 部分时,图像上绘制的形状会反转

转载 作者:行者123 更新时间:2023-12-02 01:38:35 25 4
gpt4 key购买 nike

我有一个已转换为图像的 PDF 文件。我能够在图像上书写,但是当我尝试将形状/线条保存到 pdf 中时,该点不在其位置,并且形状被反转。

这与 this 相关问题,第一部分已经得到解答。现在第二个问题是当我尝试缩放图像时。这些点不在其位置。

我发现更改比例时它不起作用。我更新了代码并将 Translate 值添加到 moveTolineTo 作为参数,当比例为默认值 72/200f 时,它以某种方式起作用。

感谢 Tilman Hausherr 的耐心等待..

注意:按住 CTRL 键然后使用鼠标滚轮放大/缩小。此事件可能会改变比例,从而扰乱我的形状的点。

package pdfwriter;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.util.Matrix;

public class SO57387803DrawShapesInPDF {

private static class MyImagePanel extends JPanel {

final BufferedImage image;
//final float scale = .38f;
final int DPI = 200;
float scale = 72f / DPI;

//NEW
double affineX;
double affineY;

AffineTransform atg;
Point start = new Point();
Point end = new Point();
boolean isNewLine = true;
static ArrayList<Line2D> lines = new ArrayList<>();
static PDDocument document;

public MyImagePanel() throws IOException {
document = PDDocument.load(new File("c:\\users\\john ebarita\\documents\\plan.pdf"));
PDFRenderer renderer = new PDFRenderer(document);

image = renderer.renderImageWithDPI(0, DPI, ImageType.RGB);

addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (end != start) {
Line2D line = new Line2D.Float(start.x, start.y, end.x, end.y);

lines.add(line);
Point2D p = calcCoordinates(e);

start = new Point();
start.x = (int) p.getX();
start.y = (int) p.getY();

repaint();
}

}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
Point2D p = calcCoordinates(e);
end = new Point();

end.x = (int) p.getX();
end.y = (int) p.getY();
repaint();
}
});
addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {

int rotation = e.getWheelRotation();

//Zoom in/out on CTRL
if (e.isControlDown()) {

if (rotation < 0) {
scale -= .05;
} else {
scale += .05;
}
if (scale < .2) {
// IMAGE_SCALE = (float) .2;
} else if (scale > 4) {
scale = 4;
}
revalidate();
repaint();
}
}
});
}

private Point2D calcCoordinates(MouseEvent e) {
Point p = new Point(e.getX(), e.getY());
try {
return atg.inverseTransform(p, null);
} catch (NoninvertibleTransformException ex) {
return p;
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2D = (Graphics2D) g.create();

// double affineX = (getWidth() - scale * image.getWidth()) / 2;
// double affineY = (getHeight() - scale * image.getHeight()) / 2;
// updated
affineX = (getWidth() - scale * image.getWidth()) / 2;
affineY = (getHeight() - scale * image.getHeight()) / 2;

AffineTransform at = new AffineTransform();
at.translate(affineX, affineY);
at.scale(scale, scale);

AffineTransform atf = g2D.getTransform();
atf.concatenate(at);
atg = (AffineTransform) at.clone();

g2D.setTransform(atf);

g2D.drawImage(image, 0, 0, this);
try {
g2D.drawLine(start.x, start.y, end.x, end.y);
} catch (NullPointerException e) {

}

for (Line2D l : lines) {
g2D.draw(l);
}

g2D.dispose();
}

public void save() {

try {
PDPage page = document.getPage(0);

PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true, true);
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(270), 0, page.getMediaBox().getHeight()));
for (Line2D l : lines) {
Point2D p1 = new Point2D.Double(l.getX1(), l.getY1());
Point2D p2 = new Point2D.Double(l.getX2(), l.getY2());

p1 = atg.transform(p1, null);
p2 = atg.transform(p2, null);

// contentStream.moveTo((float) (p1.getX()), page.getMediaBox().getWidth() - (float) (p1.getY() ));
// contentStream.lineTo((float) (p2.getX()), page.getMediaBox().getWidth() - (float) (p2.getY() ));
//UPATED
contentStream.moveTo((float) (p1.getX() - affineX), page.getMediaBox().getWidth() - (float) (p1.getY() - affineY));
contentStream.lineTo((float) (p2.getX() - affineX), page.getMediaBox().getWidth() - (float) (p2.getY() - affineY));

contentStream.stroke();
}

contentStream.close();

document.save(new File("c:\\users\\john ebarita\\documents\\saved.pdf"));
document.close();
System.out.println("file saved");
} catch (IOException ex) {
// Logger.getLogger(SO57387803DrawShapesInPDF.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}

}

@Override
public Dimension getPreferredSize() {
int width = (int) (scale * image.getWidth());
int height = (int) (scale * image.getHeight());
return new Dimension(width, height);
}
}

public static void main(String[] args) {

// System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
JFrame frame = new JFrame("PDF");
frame.setSize(1500, 1200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

// MyImagePanel imagePanel = null;
MyImagePanel imagePanel;
try {
imagePanel = new MyImagePanel();
} catch (IOException ex) {
ex.printStackTrace();
// Logger.getLogger(SO57387803DrawShapesInPDF.class
// .getName()).log(Level.SEVERE, null, ex);
return; // or there would be an uninitialized variable
}

JButton btn = new JButton("Save");

btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
imagePanel.save();
//MyImagePanel.save();
}
});

btn.setBounds(10, 0, 70, 30);

frame.add(btn);
JPanel pnl = new JPanel();
pnl.add(imagePanel);
pnl.setBounds(0, 100, 1500, 1200);
frame.add(pnl);
frame.revalidate();
frame.repaint();
frame.setVisible(true);
}

}

最佳答案

经过多次尝试和错误,我想我得到了尺度和分数问题的答案。

我从 72/200f 比例开始。所以我所做的就是在用 currentScale/firstScale 转换我划分的点之后,然后将其划分为变换后的点;

感谢 Tilman Hausherr 对我的帮助。

  p1 = atg.transform(p1, null);
p2 = atg.transform(p2, null);

contentStream.moveTo((float) (p1.getX() / (scale / .36)), (float) page.getMediaBox().getWidth() - p1.getY() / (scale / .36)));
contentStream.lineTo((float) (p2.getX() / (scale / .36)), (float) (page.getMediaBox().getWidth() - p2.getY() / (scale / .36)));

完整代码;

package pdfwriter;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.util.Matrix;

public class SO57387803DrawShapesInPDF {

private static class MyImagePanel extends JPanel {

final BufferedImage image;
//final float scale = .38f;
final int DPI = 200;
float scale = 72f / DPI;

//NEW
double affineX;
double affineY;

AffineTransform atg;
Point start = new Point();
Point end = new Point();
boolean isNewLine = true;
static ArrayList<Line2D> lines = new ArrayList<>();
static PDDocument document;

public MyImagePanel() throws IOException {
document = PDDocument.load(new File("plan.pdf"));
PDFRenderer renderer = new PDFRenderer(document);

image = renderer.renderImageWithDPI(0, DPI, ImageType.RGB);

addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (end != start) {
Line2D line = new Line2D.Float(start.x, start.y, end.x, end.y);

lines.add(line);
Point2D p = calcCoordinates(e);

start = new Point();
start.x = (int) p.getX();
start.y = (int) p.getY();

repaint();
}

}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
Point2D p = calcCoordinates(e);
end = new Point();

end.x = (int) p.getX();
end.y = (int) p.getY();
repaint();
}
});
addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {

int rotation = e.getWheelRotation();

//Zoom in/out on CTRL
if (e.isControlDown()) {

if (rotation < 0) {
scale -= .05;
} else {
scale += .05;
}
if (scale < .2) {
scale = .2f;
} else if (scale > 4) {
scale = 4;
}
revalidate();
repaint();
}
}
});
}

private Point2D calcCoordinates(MouseEvent e) {
Point p = new Point(e.getX(), e.getY());
try {
return atg.inverseTransform(p, null);
} catch (NoninvertibleTransformException ex) {
return p;
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2D = (Graphics2D) g.create();

// double affineX = (getWidth() - scale * image.getWidth()) / 2;
// double affineY = (getHeight() - scale * image.getHeight()) / 2;
// updated
affineX = (getWidth() - scale * image.getWidth()) / 2;
affineY = (getHeight() - scale * image.getHeight()) / 2;

AffineTransform at = new AffineTransform();
at.translate(affineX, affineY);
at.scale(scale, scale);

AffineTransform atf = g2D.getTransform();
atf.concatenate(at);
atg = (AffineTransform) at.clone();

g2D.setTransform(atf);
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2D.drawImage(image, 0, 0, this);
try {
g2D.drawLine(start.x, start.y, end.x, end.y);
} catch (NullPointerException e) {

}

for (Line2D l : lines) {
g2D.draw(l);
}

g2D.dispose();
}

public void save() {

try {
PDPage page = document.getPage(0);

PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true, true);
contentStream.transform(Matrix.getRotateInstance(Math.toRadians(270), 0, page.getMediaBox().getHeight()));
for (Line2D l : lines) {
Point2D p1 = new Point((int) l.getX1(), (int) l.getY1());
Point2D p2 = new Point((int) l.getX2(), (int) l.getY2());

p1 = atg.transform(p1, null);
p2 = atg.transform(p2, null);

//UPATED
System.out.println("Points");
System.out.println(p1);
System.out.println(p2);
System.out.println("Scale " + scale);

//0.36 is the default scale or 72/200f
contentStream.moveTo((float) (p1.getX() / (scale / .36)), (float) (page.getMediaBox().getWidth() - p1.getY() / (scale / .36)));

contentStream.lineTo((float) (p2.getX() / (scale / .36)), (float) (page.getMediaBox().getWidth() - p2.getY() / (scale / .36)));

contentStream.stroke();
}

contentStream.close();

document.save(new File("c:\\users\\john ebarita\\documents\\saved.pdf"));
document.close();
System.out.println("file saved");
} catch (IOException ex) {
// Logger.getLogger(SO57387803DrawShapesInPDF.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}

}

@Override
public Dimension getPreferredSize() {
int width = (int) (scale * image.getWidth());
int height = (int) (scale * image.getHeight());
return new Dimension(width, height);
}
}

public static void main(String[] args) {

// System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
JFrame frame = new JFrame("PDF");
frame.setSize(1500, 1200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

// MyImagePanel imagePanel = null;
MyImagePanel imagePanel;
try {
imagePanel = new MyImagePanel();
} catch (IOException ex) {
ex.printStackTrace();
// Logger.getLogger(SO57387803DrawShapesInPDF.class
// .getName()).log(Level.SEVERE, null, ex);
return; // or there would be an uninitialized variable
}

JButton btn = new JButton("Save");

btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
imagePanel.save();
//MyImagePanel.save();
}
});

btn.setBounds(10, 0, 70, 30);

frame.add(btn);
JPanel pnl = new JPanel();
pnl.add(imagePanel);
pnl.setBounds(0, 100, 1500, 1200);
frame.add(pnl);
frame.revalidate();
frame.repaint();
frame.setVisible(true);
}

}

关于java - 写入 PDF 文件第 2 部分时,图像上绘制的形状会反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57503325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com