I am using the below mentioned code. Due to some reasons I am not able to save the image in output PDF. is there anything I am missing ?
string imageFileName = Path.Combine(Application.StartupPath, "a.jpg");
var inputpdf = Path.Combine(Application.StartupPath, "b.pdf");
var outputpdf = Path.Combine(Application.StartupPath, "output.pdf");
using (Stream inputPdfStream = new FileStream(inputpdf, FileMode.Open, FileAccess.Read,
FileShare.Read))
{
using (Stream inputImageStream = new FileStream(imageFileName, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, new FileStream(outputpdf, FileMode.Create),
'\0', true);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(10, 10);
pdfContentByte.AddImage(image);
stamper.Close();
}
}
Related
I am trying to stamp existing pdf document using ITextSharp's stamper.
I am able to open existing pdf and put an image inside on the desired position. (stamp the pdf)
Probleme is that stamp (red image) is always under the drawing. (black lines are over the red image)
I am not able to do it vice versa.
My result:
The desired result is exactly the opposite - red image over the black lines
Any idea how to accomplish this properly?
Thx for any advice.
Here is my code:
using (Stream inputPdfStream = new FileStream(#"D:\tmp\go\input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream(#"D:\tmp\go\output.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
using (Stream inputImageStream = new FileStream(#"D:\tmp\go\stamp.png", FileMode.Open, FileAccess.Read, FileShare.Read))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
int lastPage = reader.NumberOfPages;
Image image = Image.GetInstance(inputImageStream);
image.ScalePercent(35.5f);
image.SetAbsolutePosition(30, 30);
PdfGState graphicsState = new PdfGState();
graphicsState.BlendMode = PdfGState.BM_DARKEN;
var pdfContentByte = stamper.GetOverContent(lastPage);
pdfContentByte.SetGState(graphicsState);
pdfContentByte.SaveState();
pdfContentByte.AddImage(image);
stamper.Close();
}
I have WPF application in which user enters some text in rich text box(rtb), I convert that rtb string to HTML and then convert that HTML to image and then insert it in the PDF document
using (Stream inputPdfStream = new FileStream("sample.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream("result2.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, outputPdfStream);
PdfContentByte pdfContentByte = null;
int c = reader.NumberOfPages;
iTextSharp.text.Image image = TextSharp.text.Image.GetInstance(ConvertXamltohtmltoImage(xamlstring));
foreach (var item in lst)
{
image.ScaleToFit(item._Size.Width, item._Size.Height);
image.SetAbsolutePosition(item.Location.X, item.Location.Y);
pdfContentByte = stamper.GetOverContent(item.pageNo);
pdfContentByte.AddImage(image);
}
stamper.Close();
}
My question is can I insert HTML directly into PDF?
You need an extra DLL to do that: http://sourceforge.net/projects/itextsharp/files/xmlworker/
See the demo: http://demo.itextsupport.com/xmlworker/
Unfortunately, the documentation hasn't been updated recently. We're working on it.
Is it is possible to change the following code in order to create the images in the beginning and use their references in the pages in which I want to insert the specific image?
using (Stream inputPdfStream = new FileStream("sample.pdf", 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);
PdfContentByte pdfContentByte = null;
int c = reader.NumberOfPages;
string fnmae = "";
iTextSharp.text.Image image = null;
for (int i = 1; i <= c; i++)
{
fnmae = (i % 2==0) ? "1.jpg" : "6.jpg";
image = iTextSharp.text.Image.GetInstance(fnmae);
pdfContentByte = stamper.GetOverContent(i);
image.ScaleToFit(100, 100);
image.SetAbsolutePosition(100, 100);
pdfContentByte.AddImage(image);
}
stamper.Close();
}
Below is my Code I am generating Password Protected pdf from ItextSharp.
Actually two pdf are getting generating and saving.
But i want only file to be saved.
If I use same for input and output i am getting error.
Truly appreciate your help.
Letter1 mydoc = new Letter1();
mydoc.GenerateLetter();
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string InputFile = Path.Combine(WorkingFolder, "Testing1.pdf");
FileStream f = new FileStream(InputFile, FileMode.Create);
f.Write(mydoc.DocumentBytes, 0, mydoc.DocumentBytes.Length);
f.Close();
string OutputFile = Path.Combine(WorkingFolder, "TestingOut1.pdf");
using (Stream input = new FileStream(InputFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, "abc123", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
Consider using a MemoryStream
Untested code (written in browser:)
using (MemoryStream m = new MemoryStream())
{
m.Write(mydoc.DocumentBytes, 0, mydoc.DocumentBytes.Length);
m.Seek(0, SeekOrigin.Origin);
string OutputFile = Path.Combine(WorkingFolder, "TestingOut1.pdf");
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(m);
PdfEncryptor.Encrypt(reader, output, true, "abc123", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
No need to instantiate a separate stream to read the PDF you want to encrypt. Use the PdfReader overloaded constructor that accepts a file path. Something like this:
PdfReader reader = new PdfReader(InputFile);
using (Stream output = new FileStream(
OutputFile, FileMode.Create, FileAccess.Write, FileShare.None
))
{
PdfEncryptor.Encrypt(
reader, output, true, "abc123", "secret", PdfWriter.ALLOW_SCREENREADERS
);
}
How can the copyright metadata of an existing (i.e. a pdf loaded from file or memory stream) pdf file be set using iTextSharp for C#?
Thanks a lot
The native XMP structures don't have copyright implemented (or at least they don't in a way that Adobe Reader recognizes.) To do that you can reverse engineer what Adobe kicks out and write it manually:
String inputPDF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Services.pdf");
String outputPDF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Services_Out.pdf");
PdfReader reader = new PdfReader(inputPDF);
using (FileStream fs = new FileStream(outputPDF, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
using (MemoryStream ms = new MemoryStream())
{
string CopyrightName = "YOUR NAME HERE";
string CopyrightUrl = "http://www.example.com/";
XmpWriter xmp = new XmpWriter(ms);
xmp.AddRdfDescription("xmlns:dc=\"http://purl.org/dc/elements/1.1/\"", String.Format("<dc:rights><rdf:Alt><rdf:li xml:lang=\"x-default\">{0}</rdf:li></rdf:Alt></dc:rights>", CopyrightName));
xmp.AddRdfDescription("xmlns:xmpRights=\"http://ns.adobe.com/xap/1.0/rights/\"", string.Format("<xmpRights:Marked>True</xmpRights:Marked><xmpRights:WebStatement>{0}</xmpRights:WebStatement>", CopyrightUrl));
xmp.Close();
stamper.XmpMetadata = ms.ToArray();
stamper.Close();
}
}
}