This was my code for itextsharp which worked ok. It displayed "Quote Only" in the middle of each page in a pdf file.
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath(#"~\Content\WaterMarkQuoteOnly.png"));
PdfReader readerOriginalDoc = new PdfReader(File(all, "application/pdf").FileContents);
int n = readerOriginalDoc.NumberOfPages;
img.SetAbsolutePosition(0, 300);
PdfGState _state = new PdfGState()
{
FillOpacity = 0.1F,
StrokeOpacity = 0.1F
};
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(readerOriginalDoc, ms, '\0', true))
{
for (int i = 1; i <= n; i++)
{
PdfContentByte content = stamper.GetOverContent(i);
content.SaveState();
content.SetGState(_state);
content.AddImage(img);
content.RestoreState();
}
}
//return ms.ToArray();
all = ms.GetBuffer();
}
This is my new itext 7 code, this also displays the watermark but the position is wrong. I was dismayed to see that you cant add an image to the canvas but you have to add ImageData when the position is being set on the image. The image is also way smaller and back to front.
var imagePath = Server.MapPath(#"~\Content\WaterMarkQuoteOnly.png");
var tranState = new iText.Kernel.Pdf.Extgstate.PdfExtGState();
tranState.SetFillOpacity(0.1f);
tranState.SetStrokeOpacity(0.1f);
ImageData myImageData = ImageDataFactory.Create(imagePath, false);
Image img = new Image(myImageData);
img.SetFixedPosition(0, 300);
var reader = new PdfReader(new MemoryStream(all));
var doc = new PdfDocument(reader);
int pages = doc.GetNumberOfPages();
using (var ms = new MemoryStream())
{
var writer = new PdfWriter(ms);
var newdoc = new PdfDocument(writer);
for (int i = 1; i <= pages; i++)
{
//get existing page
PdfPage page = doc.GetPage(i);
//copy page to new document
newdoc.AddPage(page.CopyTo(newdoc)); ;
//get our new page
PdfPage newpage = newdoc.GetPage(i);
Rectangle pageSize = newpage.GetPageSize();
//get canvas based on new page
var canvas = new PdfCanvas(newpage);
//write image data to new page
canvas.SaveState().SetExtGState(tranState);
canvas.AddImage(myImageData, pageSize, true);
canvas.RestoreState();
}
newdoc.Close();
all = ms.GetBuffer();
ms.Flush();
}
You are doing something strange with the PdfDocument objects, and you are also using the wrong AddImage() method.
I am not a C# developer, so I rewrote your example in Java. I took this PDF file:
And I took this image:
Then I added the image to the PDF file using transparency with the following result:
The code to do this, was really simple:
public void createPdf(String src, String dest) throws IOException {
PdfExtGState tranState = new PdfExtGState();
tranState.setFillOpacity(0.1f);
ImageData img = ImageDataFactory.create(IMG);
PdfReader reader = new PdfReader(src);
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(reader, writer);
for (int i = 1; i <= pdf.getNumberOfPages(); i++) {
PdfPage page = pdf.getPage(i);
PdfCanvas canvas = new PdfCanvas(page);
canvas.saveState().setExtGState(tranState);
canvas.addImage(img, 36, 600, false);
canvas.restoreState();
}
pdf.close();
}
For some reason, you created two PdfDocument instances. This isn't necessary. You also used the AddImage() method passing a Rectangle which resizes the image. Also make sure that you don't add the image as an inline image, because that bloats the file size.
I don't know which programming language you are using. For instance: I am not used to variables that are created using var such as var tranState. It should be very easy for you to adapt my Java code though. It's just a matter of changing lowercases into uppercases.
Related
I'm looking to convert an Image (PNG, JPEG, GIF), in the form of a byte[] to a PDF.
I'm currently using this function, which works, but cuts off the bottom of images that over a certain height or specific proportions; for example 500x2000.
Where am I going wrong here?
public byte[] ConvertImageToPDF(byte[] bytes)
{
byte[] pdfArray;
using (var memoryStream = new MemoryStream())
{
using (var pdfWriter = new PdfWriter(memoryStream))
{
var pdf = new PdfDocument(pdfWriter);
var document = new Document(pdf);
ImageData imageData = ImageDataFactory.Create(bytes);
document.Add(new Image(imageData));
document.Close();
}
pdfArray = memoryStream.ToArray();
}
return pdfArray;
}
I suppose what you want is the PdfWriter to auto-scale the Image inside the Document.
Optionally, position the Image in the center of the Page.
You can change your code setting [Image].SetAutoScale(true) and [Image].SetHorizontalAlignment(HorizontalAlignment.CENTER):
Note: I've defined aliases for iText.Layout.Properties (alias: PdfProperties) and iText.Layout.Element.Image (alias: PdfImage), to avoid conflict with other .Net assemblies that have classes and enumerators with the same exact names. Just remove them in case you don't need them at all.
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using PdfProperties = iText.Layout.Properties;
using PdfImage = iText.Layout.Element.Image;
public byte[] ConvertImageToPDF(byte[] imageBytes)
{
using (var ms = new MemoryStream()) {
using (var pdfWriter = new PdfWriter(ms)) {
var pdf = new PdfDocument(pdfWriter);
var document = new Document(pdf);
var img = new PdfImage(ImageDataFactory.Create(imageBytes))
.SetAutoScale(true)
.SetHorizontalAlignment(PdfProperties.HorizontalAlignment.CENTER);
document.Add(img);
document.Close();
pdf.Close();
return ms.ToArray();
}
}
}
You can also specify the size, in floating point units, of the Image and use the [Image].ScaleToFit() method, to scale the Image within those bounds.
Here, using a PageSize set to PageSize.A4. You can of course set different measures.
using iText.Kernel.Geom;
// [...]
var document = new Document(pdf);
var page = document.GetPageEffectiveArea(PageSize.A4);
var img = new PdfImage(ImageDataFactory.Create(imageBytes))
.ScaleToFit(page.GetWidth(), page.GetHeight())
.SetHorizontalAlignment(PdfProperties.HorizontalAlignment.CENTER);
// [...]
As I am processing my main pdf file and when I get to page 2 I start to read this Adobe Illustrator. I get this PdfException at the line origPage.CopyAsFormXObject(aiDocument); about the indirect object. I am not sure why I am getting this error because its a different file.
iText.Kernel.Exceptions.PdfException: 'Cannot copy indirect object from the document that is being written.'
const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;
string aifilePath = #"PathToAIFile.ai";
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
var aiReader = new iText.Kernel.Pdf.PdfReader(aiStream);
var aiDocument = new PdfDocument(aiReader, new PdfWriter(aimodifiedaiStream));
PdfPage origPage = aiDocument.GetFirstPage();
PdfFormXObject aifForm = origPage.CopyAsFormXObject(aiDocument);
canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}
Updated with two PdfDocument
const float imageX = 111.318f, imageY = 130.791f;
//const float imageWidth = 755.454f, imageHeight = 432.094f;
string aifilePath = #"PathToAIFile.ai";
var buffer = File.ReadAllBytes(aifilePath);
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
var aisourceReader = new iText.Kernel.Pdf.PdfReader(aiStream);
var pdfsourceDocument = new PdfDocument(aisourceReader, new PdfWriter(aimodifiedaiStream));
var pdftargetDocument = new PdfDocument(aisourceReader);
PdfPage origPage = aiDocument.GetFirstPage();
PdfFormXObject aifForm = origPage.CopyAsFormXObject(aiDocument);
canvasPage.AddXObjectAt(aifForm, imageX, imageY);
}
Updated with pdftargetDocument has a writer and pdfsourceDocument with just PdfReader still getting the error Cannot copy to document opened in reading mode.'
using (var aiStream = new MemoryStream(buffer))
using (var aimodifiedaiStream = new MemoryStream())
{
var aisourceReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
var aitargetReader = new iText.Kernel.Pdf.PdfReader(new MemoryStream(buffer));
var pdfsourceDocument = new PdfDocument(aisourceReader);
var pdftargetDocument = new PdfDocument(aitargetReader, new PdfWriter(aimodifiedaiStream));
PdfPage origPage = pdfsourceDocument.GetFirstPage();
PdfFormXObject aifForm = origPage.CopyAsFormXObject(pdfsourceDocument);
canvasPage.AddXObjectAt(aifForm, imageX, imageY);
pdfsourceDocument.Close();
pdftargetDocument.Close();
}
Summing up my comments as an answer...
You copy a page from the PdfDocument aiDocument as form XObject to the same aiDocument.
Clearly aiDocument is being written, it has a PdfWriter in its constructor. But the iText API only allows copying pages from a PdfDocument constructed without a PdfWriter.
Furthermore, you can only copy to a PdfDocument that is written to, i.e. that is constructed with a PdfWriter. Thus, you cannot copy from a document into itself.
Thus, you need different PdfDocument instances as source and target of the copy process.
The source PdfDocument instance is generated with only a PdfReader, no PdfWriter. The target PdfDocument instance is generated with a PdfWriter. If the target also is generated with a PdfReader depends on your precise use case: Do you want to add the XObject to an existing document, or do you want to add it to an empty document without any prior content.
I want to insert image to the user upload file, when I tried to create pdfDocument from the stream I got the "You can't read from OutputStream" error
public ActionResult GetTemplate(IFormFile template)
{
byte[] pdfBytes;
using (var memoryStream = new MemoryStream())
{
template.CopyTo(memoryStream);
var writer = new PdfWriter(memoryStream);
PdfDocument pdfDocument = new PdfDocument(new PdfReader(memoryStream));
var document = new Document(pdfDocument);
var pageCount = pdfDocument.GetNumberOfPages();
ImageData imageData = ImageDataFactory.CreatePng(System.Convert.FromBase64String(//));
var img = new Image(imageData).Scale(200, 100).SetFixedPosition(pageCount, 50, 120);
document.Add(img);
document.Close();
pdfBytes = memoryStream.ToArray();
}
return new FileContentResult(pdfBytes, "application/pdf");
}
If I changed it to new PdfDocument(new PdfReader(writer)); I got "PDF header not found" error instead.
This is my first time using iText, most of the answers I found on their website only worked with java. Any suggestion or help would be great, thank you for your time.
I have this code that will add a watermark on each page:
string watermarkLocation = AppDomain.CurrentDomain.BaseDirectory + "Watermark.png";
Document document = new Document();
PdfReader pdfReader = new PdfReader(fileLocation);
PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(fileLocation.Replace(".pdf", "_marked.pdf"), FileMode.Create));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(watermarkLocation);
img.ScaleToFit(document.PageSize);
img.SetAbsolutePosition(0, 100);
PdfContentByte waterMark;
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
waterMark = stamp.GetOverContent(page);
waterMark.AddImage(img);
}
stamp.FormFlattening = true;
stamp.Close();
return fileLocation.Replace(".pdf", "_marked.pdf");
But on PDFs that have textboxes, the image will go behind the textbox/form. I thought flattening the file will fix this, but it does not work.
I used a full image as a test but the watermark in the end will have transparency.
Here's the final code I'm using. As my comment mentioned, there's basically 2 readers/stamps, one to flatten the file and another to add the watermark.
Flatten file:
private byte[] FlattenPdfFormToBytes(PdfReader reader)
{
var memStream = new MemoryStream();
var stamper = new PdfStamper(reader, memStream) { FormFlattening = true };
stamper.Close();
return memStream.ToArray();
}
Add Watermark (which will call FlattenPdfFormToBytes):
public string AddWatermark(string fileLocation)
{
string watermarkLocation = AppDomain.CurrentDomain.BaseDirectory + "Watermark.png";
Document document = new Document();
PdfReader pdfReader = new PdfReader(fileLocation);
PdfReader pdfFlatten = new PdfReader(FlattenPdfFormToBytes(pdfReader)); // The secret sauce is this!!!
PdfStamper stamp = new PdfStamper(pdfFlatten, new FileStream(fileLocation.Replace(".pdf", "_marked.pdf"), FileMode.Create));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(watermarkLocation);
img.ScaleToFit(document.PageSize);
img.SetAbsolutePosition(0, 100);
PdfContentByte waterMark;
for (int page = 1; page <= pdfFlatten.NumberOfPages; page++)
{
waterMark = stamp.GetOverContent(page);
waterMark.AddImage(img);
}
stamp.Close();
return fileLocation.Replace(".pdf", "_marked.pdf");
}
We are using itextsharp to create a single PDF from multiple PDF files. How do I insert a new page into a PDF file that has multiple pages already in the file? When I use add page it is overwriting the existing pages and only saves the 1 page that was selected.
Here is the code that I am using to add the page to the existing PDF:
PdfReader reader = new PdfReader(sourcePdfPath);
Document document = new Document(reader.GetPageSizeWithRotation(1));
PdfCopy pdfCopy = new PdfCopy(document, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.AddDocListener(writer);
document.Open();
for (int p = 1; p <= reader.NumberOfPages; p++)
{
if (pagesToExtract.FindIndex(s => s == p) == -1) continue;
document.SetPageSize(reader.GetPageSize(p));
document.NewPage();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage pageImport = writer.GetImportedPage(reader, p);
int rot = reader.GetPageRotation(p);
if (rot == 90 || rot == 270)
{
cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
}
else
{
cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
}
pdfCopy.AddPage(pageImport);
}
pdfCopy.Close();
This code works. You need to have a different file to output the results.
private static void AppendToDocument(string sourcePdfPath1, string sourcePdfPath2, string outputPdfPath)
{
using (var sourceDocumentStream1 = new FileStream(sourcePdfPath1, FileMode.Open))
{
using (var sourceDocumentStream2 = new FileStream(sourcePdfPath2, FileMode.Open))
{
using (var destinationDocumentStream = new FileStream(outputPdfPath, FileMode.Create))
{
var pdfConcat = new PdfConcatenate(destinationDocumentStream);
var pdfReader = new PdfReader(sourceDocumentStream1);
var pages = new List<int>();
for (int i = 0; i < pdfReader.NumberOfPages; i++)
{
pages.Add(i);
}
pdfReader.SelectPages(pages);
pdfConcat.AddPages(pdfReader);
pdfReader = new PdfReader(sourceDocumentStream2);
pages = new List<int>();
for (int i = 0; i < pdfReader.NumberOfPages; i++)
{
pages.Add(i);
}
pdfReader.SelectPages(pages);
pdfConcat.AddPages(pdfReader);
pdfReader.Close();
pdfConcat.Close();
}
}
}
}
I've tried this code, and it works for me, but don't forget to do some validations of the number of pages and existence of the paths you use
here is the code:
private static void AppendToDocument(string sourcePdfPath, string outputPdfPath, List<int> neededPages)
{
var sourceDocumentStream = new FileStream(sourcePdfPath, FileMode.Open);
var destinationDocumentStream = new FileStream(outputPdfPath, FileMode.Create);
var pdfConcat = new PdfConcatenate(destinationDocumentStream);
var pdfReader = new PdfReader(sourceDocumentStream);
pdfReader.SelectPages(neededPages);
pdfConcat.AddPages(pdfReader);
pdfReader.Close();
pdfConcat.Close();
}
You could use something like this, where src is the IEnumerable<string> of input pdf filenames. Just make sure that your existing pdf file is one of those sources.
The PdfConcatenate class is in the latest iTextSharp release.
var result = "combined.pdf";
var fs = new FileStream(result, FileMode.Create);
var conc = new PdfConcatenate(fs, true);
foreach(var s in src) {
var r = new PdfReader(s);
conc.AddPages(r);
}
conc.Close();
PdfCopy is intended for use with an empty Document. You should add everything you want, one page at a time.
The alternative is to use PdfStamper.InsertPage(pageNum, rectangle) and then draw a PdfImportedPage onto that new page.
Note that PdfImportedPage only includes the page contents, not the annotations or doc-level information ("document structure", doc-level javascripts, etc) that page may have originally used... unless you use one with PdfCopy.
A Stamper would probably be more efficient and use less code, but PdfCopy will import all the page-level info, not just the page's contents.
This might be important, it might not. It depends on what page you're trying to import.
Had to even out the page count with a multiple of 4:
private static void AppendToDocument(string sourcePdfPath)
{
var tempFileLocation = Path.GetTempFileName();
var bytes = File.ReadAllBytes(sourcePdfPath);
using (var reader = new PdfReader(bytes))
{
var numberofPages = reader.NumberOfPages;
var modPages = (numberofPages % 4);
var pages = modPages == 0 ? 0 : 4 - modPages;
if (pages == 0)
return;
using (var fileStream = new FileStream(tempFileLocation, FileMode.Create, FileAccess.Write))
{
using (var stamper = new PdfStamper(reader, fileStream))
{
var rectangle = reader.GetPageSize(1);
for (var i = 1; i <= pages; i++)
stamper.InsertPage(numberofPages + i, rectangle);
}
}
}
File.Delete(sourcePdfPath);
File.Move(tempFileLocation, sourcePdfPath);
}
I know I'm really late to the part here, but I mixed a bit of the two best answers and created a method if anyone needs it that adds a list of source PDF documents to a single document using itextsharp.
private static void appendToDocument(List<string> sourcePDFList, string outputPdfPath)
{
//Output document name and creation
FileStream destinationDocumentStream = new FileStream(outputPdfPath, FileMode.Create);
//Object to concat source pdf's to output pdf
PdfConcatenate pdfConcat = new PdfConcatenate(destinationDocumentStream);
//For each source pdf in list...
foreach (string sourcePdfPath in sourcePDFList)
{
//If file exists...
if (File.Exists(sourcePdfPath))
{
//Open the document
FileStream sourceDocumentStream = new FileStream(sourcePdfPath, FileMode.Open);
//Read the document
PdfReader pdfReader = new PdfReader(sourceDocumentStream);
//Create an int list
List<int> pages = new List<int>();
//for each page in pdfReader
for (int i = 1; i < pdfReader.NumberOfPages + 1; i++)
{
//Add that page to the list
pages.Add(i);
}
//Add that page to the pages to add to ouput document
pdfReader.SelectPages(pages);
//Add pages to output page
pdfConcat.AddPages(pdfReader);
//Close reader
pdfReader.Close();
}
}
//Close pdfconcat
pdfConcat.Close();
}