我正在尝试从JPEG,BMP类型的图像生成PDF但是我得到了右边图像的一部分总是被切断。我正在使用一个默认的Windows图片Sunset.jpg。
以下是代码:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcitt;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
public class ImageToPDF
{
public void createPDFFromImage( String file, String image) throws IOException, COSVisitorException
{
PDDocument doc = null;
try
{
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage( page );
PDXObjectImage ximage = null;
if( image.toLowerCase().endsWith( ".jpg" ) || image.toLowerCase().endsWith( ".jpeg" ))
{
BufferedImage awtImage = ImageIO.read( new File( image ) );
ximage = new PDJpeg(doc, awtImage, 0 );
}
else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
{
ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
}
else
{
BufferedImage awtImage = new BufferedImage(1000, 800, BufferedImage.TYPE_INT_RGB);
awtImage = ImageIO.read(new FileImageInputStream(new File( image )));
ximage = new PDPixelMap(doc, awtImage);
}
System.out.println(" Width of the image.... "+ximage.getWidth());
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.drawImage( ximage, 20, 20 );
contentStream.close();
doc.save( file );
}
finally
{
if( doc != null )
{
doc.close();
}
}
}
public static void main(String[] args)
{
ImageToPDF app = new ImageToPDF();
try
{
app.createPDFFromImage( "C:\\test1.pdf", "C:\\Sunset.jpg");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
请帮我纠正我做错了什么。
答案 0 :(得分:8)
这些代码可能对您有所帮助,它有效。
public void createPDFFromImage(String pdfFile,
List<String> imgList,int x, int y, float scale) throws IOException, COSVisitorException {
// the document
PDDocument doc = null;
try {
doc = new PDDocument();
Iterator iter = imgList.iterator();
int imgIndex=0;
while(iter.hasNext()) {
PDPage page = new PDPage();
doc.addPage(page);
BufferedImage tmp_image = ImageIO.read(new File(iter.next().toString()));
BufferedImage image = new BufferedImage(tmp_image.getWidth(), tmp_image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
image.createGraphics().drawRenderedImage(tmp_image, null);
PDXObjectImage ximage = new PDPixelMap(doc, image);
imgIndex++;
PDPageContentStream contentStream = new PDPageContentStream(
doc, page,true,true);
contentStream.drawXObject(ximage, x, y, ximage.getWidth()*scale, ximage.getHeight()*scale);
contentStream.close();
}
doc.save(pdfFile);
} finally {
if (doc != null) {
doc.close();
}
}
}
答案 1 :(得分:0)
您是否考虑裁剪图像以防止其被切断?
contentStream.drawImage( ximage, 20, 20, croppedWidth, croppedHeight );
答案 2 :(得分:0)
请尝试修改drawImage语句,如下所示:
content.drawImage(ximage,0 /*or your preferred indent*/,(700-ximage.getHeight()));
答案 3 :(得分:0)
请添加以下代码段并尝试:
需要导入:
import org.apache.pdfbox.pdmodel.common.PDRectangle;
代码添加:
PDXObjectImage image = new PDJpeg(pdfDoc, filePath));
float w = image.getWidth();
float h = image.getHeight();
PDPage page = new PDPage(new PDRectangle (w,h));
答案 4 :(得分:0)
这是一个工作例程(使用PDFBox 2.0.x版),将BufferedImage(例如签名)添加到以Byte数组形式给出的PDF文档上。结果再次是字节数组。
<telerik:RadTimeBar x:Name="PoCTimeBar" Width="450"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Background="#FF435061"
BorderBrush="{x:Null}"
BorderThickness="1,0,1,1"
Content=""
Cursor="Hand"
EnableSparklineVirtualization="False"
PeriodEnd="31-Dec-2019 11:59:59 PM"
PeriodStart="1-Jan-2019 12:00:00 AM"
ScrollMode="None"
SelectionEnd="{Binding Tab.CurrentPoC.PickEnd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionStart="{Binding Tab.CurrentPoC.PickStart, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectionTitleFormatString="{}{0:dd/MM/yy-HH:mm} - {1:dd/MM/yy-HH:mm}"
VisiblePeriodEnd="{Binding Tab.CurrentPoC.EndDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VisiblePeriodStart="{Binding Tab.CurrentPoC.StartDate}">
<telerik:RadTimeBar.Resources>
<Style TargetType="telerik:ItemControl">
<Setter Property="Foreground" Value="Red" />
</Style>
<Style TargetType="telerik:GroupControl">
<Setter Property="Foreground" Value="Blue" />
</Style>
</telerik:RadTimeBar.Resources>
<telerik:RadTimeBar.Intervals>
<telerik:HourInterval />
<telerik:DayInterval />
<telerik:MonthInterval />
<telerik:YearInterval />
</telerik:RadTimeBar.Intervals>
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<Custom:EventToCommand Command="{Binding Tab.IRefreshSpanPoC, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>-->
</telerik:RadTimeBar>
希望这会有所帮助。干杯!