iTextPDF(Java/Android): How to convert Image to bitmap [duplicate] - java

Please let me know what method can be used to convert pdf to image in iText7.
In Itexsharp, there was an option to convert pdf file to images. Following is the link. PDF to Image Using iTextSharp
http://www.c-sharpcorner.com/UploadFile/a0927b/create-pdf-document-and-convert-to-image-programmatically/
Below is the sample code created using the following refernce link.
itext7 pdf to image
this is not working as expected. It is not converting the pdf to image. It is creating a 1kb blank image.
string fileName = System.IO.Path.GetFileNameWithoutExtension(inputFilePath);
var pdfReader = new PdfReader(inputFilePath);
var pdfDoc = new iText.Kernel.Pdf.PdfDocument(pdfReader);
int pagesLength = pdfDoc.GetNumberOfPages()+1;
for (int i = 1; i < pagesLength; i++)
{
if (!File.Exists(System.IO.Path.Combine(imageFileDir, fileName + "_" +
`enter code here`(startIndex + i) + ".png")) && i < pagesLength)
{
PdfPage pdfPages = pdfDoc.GetPage(i);
PdfWriter writer = new PdfWriter(System.IO.Path.Combine(imageFileDir, fileName + "_" + (startIndex + i) + ".png"), new WriterProperties().SetFullCompressionMode(true));
PdfDocument pdf = new PdfDocument(writer);
PdfFormXObject pageCopy = pdfPages.CopyAsFormXObject(pdf);
iText.Layout.Element.Image image = new iText.Layout.Element.Image(pageCopy);
}
}

Quoting Bruno:
iText does not convert PDFs to raster images (such as .jpg, .png,...). You are misinterpreting the examples that create an Image instance based on an existing page. Those examples create an XObject that can be reused in a new PDF as if it were a vector image; they don't convert a PDF page to a raster image.
What you can use for this (which is what we at iText internally use for testing) is GhostScript. It takes a pdf as input and converts it to a series of images (one image per page).

Related

itext 7 (java) add image on a new page to the end of an existing pdf document

i'm new to itext 7. I have a list of documents with different content that I want to merge into a single PDF. The content types are PDF, JPG & PNG.
My problem is that as soon as I merge a PDF and an image, the image is written over the already inserted content of the target PDF.
How do I get each image to be added to a new page of the target PDF?
This is my Code:
byte[] mergeInhalt(List<Dokument> dokumentList) throws IOException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(byteOut);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document completeDocument = new Document(pdfDocument);
for (Dokument dokument : dokumentList) {
byte[] inhalt = dokument.getInhalt();
if (Objects.nonNull(inhalt)) {
switch (dokument.getFormat().name()) {
case "PDF":
addPdf(pdfDocument, inhalt);
break;
case "JPG":
case "PNG":
ImageData data = ImageDataFactory.create(inhalt);
Image image = new Image(data);
completeDocument.add(image);
break;
}
}
}
completeDocument.close();
return byteOut.toByteArray();
}
private void addPdf(PdfDocument pdfDocument, byte[] inhalt) throws IOException {
PdfReader pdfReader = new PdfReader(new ByteArrayInputStream(inhalt));
PdfDocument pdfDocumentToMerge = new PdfDocument(pdfReader);
pdfDocumentToMerge.copyPagesTo(1, pdfDocumentToMerge.getNumberOfPages(), pdfDocument);
}
Merging PDFs only works well, but everytime i merge a Image i get this:
The image with the pink one was placed over the text of the previous PDF
In your code you add the image via the Document completeDocument but you add the pdf via the underlying PdfDocument pdfDocument. Thus, the completeDocument doesn't know about the added pdf, continues on its current page, and draws over the imported pages.
To make sure each image is added on a new page after the currently last one, you have to tell completeDocument to move its current page:
case "JPG":
case "PNG":
ImageData data = ImageDataFactory.create(inhalt);
Image image = new Image(data);
completeDocument.add(new AreaBreak(AreaBreakType.LAST_PAGE));
completeDocument.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
completeDocument.add(image);
break;
For an example compare the tests testMergeLikeAndreasHuber and testMergeLikeAndreasHuberImproved in CopyPdfsAndImages and their outputs.

Read tick boxes given as images in PDF using iText

I have a PDF form where there are check boxes given as images in the PDF. I am using itext to read the PDF, I also want to get the value of the checkbox as ticked or unticked.
iText does not return anything for the images, below is the iText code:
PdfReader reader = new PdfReader(path);
Rectangle mediaboxKeys=reader.getPageSize(i);
mediaboxKeys.setRight((float) 100.00);
RenderFilter[] filterKeys = {new RegionTextRenderFilter(mediaboxKeys)};
FilteredTextRenderListener strategyKeys = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filterKeys);
String[] keysFromPage = PdfTextExtractor.getTextFromPage(reader, i, strategyKeys).split("\\r?\\n");
Attached is the pdf : https://drive.google.com/file/d/1D9TNnHZe5kqwv6LKIVO94Am1nl8AnrB1/view

pdf to .tiff conversion using java

I have created a web service that has a method which takes in a parameter of DataHandler. The purpose of this method is to read in what is sent over DataHandler and write it to a file(.tiff). When I pass in a .tiff file i can easily make a conversion but how should I make a conversion from a pdf file to a .tiff file using Java.
So if a user passes in a pdf file using DataHandler, how can I convert that to a .tiff file?
PDDocument doc = PDDocument.loadNonSeq(new File(filename), null);
boolean b;
List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
for (int p = 0; p < pages.size(); ++p)
{
// RGB image with 300 dpi
BufferedImage bim = pages.get(p).convertToImage(BufferedImage.TYPE_INT_RGB, 300);
// alternatively: B/W image with 300 dpi
bim = pages.get(p).convertToImage(BufferedImage.TYPE_BYTE_BINARY, 300);
// save as TIF with dpi in the metadata
// PDFBox will choose the best compression for you
// you need to add jai_imageio to your classpath for this to work
b = ImageIOUtil.writeImage(bim, "page-" + (p+1) + ".tif", 300);
if (!b)
{
// error handling
}
}
If your question is about converting to a multipage TIFF, please say so, I have a solution for this too.

droidtext adding image doesn't work

I am desperately trying to insert an image into an existing pdf with droidtext.
The original version of this project was made with iText.
So the code already exists and was modified to fit for Android.
What I do is I take an existing PDF as background.
Insert text and crosses at specified positions within this pdf.
Like filling out a form.
This works quite well so far without changing the code drastically.
Now I want to set an image to the bottom of the page to sign the form.
I used my original code and adapted it a little.
Which doesn't work at all.
I tried to set the image at a specific position. Maybe that was the error.
So i tried to do it the "official" way.
The Pengiuns.jpg image is located on the sd-card.
try {
Document document = new Document();
File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf");
PdfWriter.getInstance(document,new FileOutputStream(f));
document.open();
document.add(new Paragraph("Simple Image"));
String path = Environment.getExternalStorageDirectory()+"/Penguins.jpg";
if (new File(path).exists()) {
System.out.println("filesize: " + path + " = " + new File(path).length());
}
Image image =Image.getInstance(path);
document.add(image);
document.close();
} catch (Exception ex) {
System.out.println("narf");
}
But still no image at all.
What I get is an PDF with the words "Simple Image" on it and nothing else.
I can access the picture. I get the correct filesize by the if().
No exceptions are thrown.
So my questions are, how do I get an Image located on the SD-Card into my pdf?
What is the mistake here?
But most importantly how do I set the image to a specific location with size within the pdf?
In my original code i use setAbsolutePosition( x, y ) for that.
Eclipse is not complaining when I use it in the code but is it really working?
The reason why you are getting the "Simple Image" is because you have it in Paragraph. In order to add an image, use:
Image myImg1 = Image.getInstance(stream1.toByteArray());
If you want to have it as a footer in the last page you can use the following code but it works with text. You can try to manipulate it for image:
Phrase footerText = new Phrase("THIS REPORT HAS BEEN GENERATED USING INSPECTIONREPORT APP");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);
Here is sample code. Here I have uploaded an image to Imageview and then added to pdf. Hope this helps.
private String NameOfFolder = "/InspectionReport";
Document doc = new Document();
try { //Path to look for App folder
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder;
String CurrentDateAndTime= getCurrentDateAndTime();
// If App folder is not there then create one
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
//Creating new file and naming it
int i = 1;
File file = new File(dir, "Inspection"+Integer.toString(i)+"-" + CurrentDateAndTime+".pdf");
while(file.exists()) {
file = new File(dir, "Inspection"+Integer.toString(i++)+"-" + CurrentDateAndTime+".pdf");}
String filep= file.toString();
FileOutputStream fOut = new FileOutputStream(file);
Log.d("PDFCreator", "PDF Path: " + path);
PdfWriter.getInstance(doc, fOut);
Toast.makeText(getApplicationContext(),filep , Toast.LENGTH_LONG).show();
//open the document
doc.open();
ImageView img1 = (ImageView)findViewById(R.id.img1);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
Bitmap bitmap1 = ((BitmapDrawable)img1.getDrawable()).getBitmap();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100 , stream1);
Image myImg1 = Image.getInstance(stream1.toByteArray());
myImg1.setAlignment(Image.MIDDLE);
doc.add(myImg1);
Try following code:
/* Inserting Image in PDF */
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
//add image to document
doc.add(myImg);

extracting names of the images embedded inside a pdf

I have a pdf document containing several images.
I want to retrieve names of these images.
How to achieve this using either iText or pdfbox?
I know that ExtractImages extracts images from PDF. I feel that this will somewhere have the functionality to fetch name of the image. However, I don't know the usage of ExtractImages.
The actual problem to fetch names of PDF is to use it to compress these images to reduce the size of the pdf. Is my approach correct?
What you can get with pdfbox is the key of the image and its suffix (type). You can also save that image.
String prefix = new File(pdfFilename).getName();
prefix = prefix.substring(0, prefix.indexOf(".pdf"));
PDDocument document = null;
try
{
document = PDDocument.loadNonSeq(new(pdfFilename), null); // use non-seq parser is better
List<PDPage> pages = document.getDocumentCatalog().getAllPages();
System.out.println(pdfFilename + ": Total pages: " + pages.size());
int p = 0;
for (PDPage page : pages)
{
++p;
PDResources resources = page.getResources();
Map<String, PDXObjectImage> imageResources = resources.getImages();
for (String key : imageResources.keySet())
{
PDXObjectImage objectImage = imageResources.get(key);
System.out.printf("image key '%s': %d x %d, type %s%n", key, objectImage.getHeight(), objectImage.getWidth(), objectImage.getSuffix());
// write that image
String fname = String.format("%s-%04d-%s", prefix, p, key);
objectImage.write2file(fname);
}
}
}
// put catch here
document.close();
However this won't help you unless you are sure that all these images were converted directly to PDF, i.e. without rotation, translation or scaling. If you need this, then you might want to have a look at the PrintImageLocations.java example in the PDFBOX src download.

Categories

Resources