Add image in pdf using itext - spring

I'am creating Spring MVC web application.My image is in folder webapp/resources/img/logo.png of my MVC application.How to load image from that folder to pdf.
I tried with this code.But its trowing java.io.FileNotFoundException.
String imageUrl = "webapp/resources/img/logo.png"
logo = Image.getInstance(imageUrl);

Enhanced you find a example how I does it:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:\\test.pdf"));
document.open();
Image img = Image.getInstance(ClassLoader.getSystemResource("attention-icon.jpg"));
img.scaleAbsolute(10, 10);
Phrase phrase = new Phrase();
phrase.add(new Chunk(img, 0,0));
document.add(new Paragraph(phrase));
document.close();
I think I your case the file "webapp/resources/img/logo.png" is to relativ. Try to create a File Object to check its location:
File logo = new File("webapp/resources/img/logo.png");
if(! logo.exists()){
LOG.warn("File " + logo.getName() + " not exists");
}

Related

Itext7 generate Pdfa java

Cannot close document with already flushed PDF Catalog.
You know what this error means. It happens when I use the command with Itext7
document.close() or PdfDocument.close
I've searched the internet and here but can't find any documentation .
pdfWriter = new PdfWriter(this.path_documento + this.nome_documento);
PdfOutputIntent pdfOutputIntent = new PdfOutputIntent("Custom", "", "https://www.color.org","sRGB2014", new FileInputStream("C:\\Users\\UC9001309\\Documents\\NetBeansProjects\\GestionePdf\\sRGB2014.icc") );
pdfDocument = new PdfADocument(pdfWriter, PdfAConformanceLevel.PDF_A_1A,pdfOutputIntent);
document = new Document(pdfDocument);
PdfFont font = PdfFontFactory.createFont(StandardFonts.COURIER_BOLD);
pdfDocument.setTagged();
pdfDocument.getCatalog().setLang(new PdfString("it-IT"));
pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdfDocument.getDocumentInfo();
info.setTitle("Pdf generato da GestPdf");
The code above is creating the pdfa then there are adding 2 pages and then calling the methods
document.close () or PdfDocument.colose

Add image into a word .docx document header using Apache POI XWPF

Can anyone please help me to add image into word document (.docx) header using Apache POI's XWPF component?
XWPFDocument doc = new XWPFDocument();
FileInputStream fis = new FileInputStream("/images/image.png");
XWPFParagraph par1 = doc.createParagraph();
XWPFRun runAppName = par1.createRun();
runAppName.addPicture(fis, Document.PICTURE_TYPE_PNG, "image.png", 257, 185);
doc.write(new FileOutputStream(new File("/folder/document.docx")));

When merging PDF documents using iText, the image fields are missing

I had a PDF document and needed to add a couple of image fields. I imported the pdf file into Adobe LiveCycle as artwork. I then added my 2 images and saved as a static file. When I view the file using Adobe Reader, I can see the images. But when I try to merge the file with other static and dynamic pdf files created using LiveCycle, the images are missing.
I looked at the following thread
Images (imageField) are not shown after iText PDF Merging
but after checking the solution shown there against my code, I am already using PdfCopy instead of PdfWriter:
ByteArrayOutputStream output = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(baosList.get(0).toByteArray());
Document document = new Document(reader.getPageSizeWithRotation(1));
reader.close();
PdfCopy writer = new PdfCopy(document, output);
document.open();
for (ByteArrayOutputStream baos : baosList)
{
// copy content
reader = new PdfReader(baos.toByteArray());
for (int idx = 1; idx <= reader.getNumberOfPages(); idx++)
writer.addPage(writer.getImportedPage(reader, idx));
reader.close();
baos.close();
}
I have other dynamic PDF files with images that are fine. I wonder if my problem is because I imported the original file as artwork.

Generate a pdf file and send after querying database for a pass mark

My project is almost done, and thanks to stackoverflow. Now that I have managed to capture Users details and their certificates, I am looking for a way to generate pdf which I will send to those who passed.
I am not looking for the code at the moment. I have an asp.net mvc 3 application which shows Certificates details for my students. Now I want the certificates to be generated then sent by email, all this automated.
First I would like help on how I can generate a pdf from database values and sending the generated pdf wont be a problem.
Using iTextSharp, this code will create and serve a PDF:
public FileStreamResult DownloadPDF()
{
MemoryStream workStream = new MemoryStream();
using(Document document = new Document())
{
PdfWriter.GetInstance(document, workStream).CloseStream = false;
document.Open();
document.SetPageSize(PageSize.LETTER);
document.SetMargins(12, 12, 8, 7);
document.NewPage();
// Create a new Paragraph object with the text, "Hello, World!"
var welcomeParagraph = new Paragraph("Hello, World!");
// Add the Paragraph object to the document
document.Add(welcomeParagraph);
// This is where your data would go
document.Close();
}
workStream.Position = 0;
FileStreamResult fileResult = new FileStreamResult(workStream, "application/pdf");
fileResult.FileDownloadName = "test.pdf";
return fileResult;
}
For more information see Creating PDF Documents with ASP.NET and iTextSharp
There are a lot of tutorials online, but this should get you started.

How can I insert an image with iTextSharp in an existing PDF?

I have an existing PDF and I can use FdFWriter to input to text boxes. It works well. Now I have an image. I have read the documentation and looked at many examples but they all create new documents and insert an image. I want to take an existing PDF and insert an image into either an image field or as the icon image of a button. I have tried but it corrupts the document.
I need to be able to take an existing document and put an image on it. I do not want to open, read, replace, and delete the original. This original changes and the name "original" only means the source file in this context. There are many PDF files like this that need an image.
Thank you for any help.
Edit - I am very thankful for the code below. It works great, but the problem for me is that the existing PDF has digital signatures on it. When the document is copied like this (into result.pdf) those signatures, while still present, have a different byte count or other item that is corrupted. This means the signatures, while they show up on result.pdf, have an icon next to them that state "invalid signature."
In case it matters I am using a Topaz signature pad to create my signatures, which has it's own security. Merely copying the PDF will not corrupt it but the process below will.
I am trying to put the image on the existing document, not a copy of it, which in this case matters.
Also, by signature, I mean handwritten, not pin numbers.
Thank you again.
EDIT - Can PdfSignatureAppearance be used for this?
EDIT - I seem to be able to do it with:
var stamper = new PdfStamper(reader, outputPdfStream,'1',true);
If you want to change the contents of an existing PDF file and add extra content such as watermarks, pagenumbers, extra headers, PdfStamper is the object you need. I have successfully used the following code to insert an image into an existing pdf file to a given absolute position:
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
class Program
{
static void Main(string[] args)
{
using (Stream inputPdfStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream inputImageStream = new FileStream("some_image.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
Image image = Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(image);
stamper.Close();
}
}
}
When you insert the image you have the possibility to resize it. You can take a look at transformation matrix in the iTextSharp documentation.
Here is a similar example whichi inserts an image on the page using the stamper:
Gmane iTex Mailing List Post
I could solve my problem by simply adding following lines to my signing code to add image
var image = iTextSharp.text.Image.GetInstance(#"C:\Users\sushil\Documents\sansign.jpg");
appearance.Acro6Layers = true;
appearance.SignatureGraphic = image;
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
As I was signing document with visible digital signature , now I can have both image and digital signature properties side by side
in the .net core6 that uses DDD try this declare class in Infrastructure Layer
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public async Task<string> SignatureToPdf(string pathPdfFile, string
pathSignatureImage, string pathOutputName)
{
var webRootPath = hostingEnvironment.ContentRootPath;
if (!File.Exists(Path.Combine(webRootPath, pathPdfFile))) return
null;
await using Stream inputPdfStream =
new FileStream(Path.Combine(webRootPath, pathPdfFile),
FileMode.Open, FileAccess.Read, FileShare.Read);
await using Stream inputImageStream =
new FileStream(Path.Combine(webRootPath, pathSignatureImage), FileMode.Open, FileAccess.Read, FileShare.Read);
await using Stream outputPdfStream =
new FileStream(Path.Combine(webRootPath, pathOutputName),
FileMode.Create, FileAccess.Write, FileShare.None);
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
var pdfContentByte = stamper.GetOverContent(1);
var image = Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(image);
stamper.Close();
return "ok";
}
pdftk can do this. It's not a library but you can easily call it from your code as a .exe.
See stamp and background commands:
http://www.pdflabs.com/docs/pdftk-man-page/
ref: How to do mail merge on top of a PDF?

Resources