The following code is used for watermarking pdf :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WaterDocument
{
class Program
{
static void Main(string[] args)
{
string FileLocation = "C:\\Users\\Desktop\\Hello.pdf";
// string watermarkedFile = "Watermarked.pdf";
// Creating watermark on a separate layer
// Creating iTextSharp.text.pdf.PdfReader object to read the Existing PDF Document
PdfReader reader1 = new PdfReader(FileLocation);
using (FileStream fs = new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create))
// Creating iTextSharp.text.pdf.PdfStamper object to write Data from iTextSharp.text.pdf.PdfReader object to FileStream object
using (PdfStamper stamper = new PdfStamper(reader1, fs))
{
// Getting total number of pages of the Existing Document
int pageCount = reader1.NumberOfPages;
// Create New Layer for Watermark
PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
// Loop through each Page
for (int i = 1; i <= pageCount; i++)
{
// Getting the Page Size
Rectangle rect = reader1.GetPageSize(i);
// Get the ContentByte object
PdfContentByte cb = stamper.GetUnderContent(i);
// Tell the cb that the next commands should be "bound" to this new layer
cb.BeginLayer(layer);
cb.SetFontAndSize(BaseFont.CreateFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
PdfGState gState = new PdfGState();
gState.FillOpacity = 0.25f;
cb.SetGState(gState);
cb.SetColorFill(BaseColor.BLACK);
cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Confidential", rect.Width / 2, rect.Height / 2, 45f);
cb.EndText();
// Close the layer
cb.EndLayer();
}
stamper.Close();
}
File.Delete(FileLocation); //error on this line
File.Move(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileLocation);
}
}
}
On this line I am getting the error
File.Delete(FileLocation);
It is throwing me the following error
The process cannot access the file 'C:\Users\Desktop\Hello.pdf' because it is being used by another process.
How can I find the problem in my code?
You're not calling Close on the PdfReader object reader1, add this before the File.Delete call:
reader1.Close()
You should also consider adding a curly brace after this line:
using (FileStream fs = new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create))
There's a risk you add a line directly below that line and your code will stop working as the using statement is terminated directly on the row below.
Related
I'm creating an application in c# for generation of some pdf files with bar code in it.
I'm using the memory stream and placing data to the table (one column) which I'll write to the pdf file at the end.
Everything is working fine till I need to add bar code. For that I use direct content.
pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream);
PdfContentByte cb = new PdfContentByte(pdfWriter);
iTextSharp.text.Image bc128 = code128.CreateImageWithBarcode(cb, null, null);
After I create bar code I add it to the table using:
tableout.AddCell(image128);
And then I have a problem. Rows from the table that I've created before are shown, but I don't see a text. If I comment line with bar code addition everything is OK.
I've found one comment on forum:
Yep. Foreground text color is defined by the Font for the high-level
text layout code. When you start using a PdfContentByte directly, the
font and color become separated, but not until then.
How can I solve this?
I know that I can use system drawning method but image with bar code is not so clear.
PdfContentByte is for direct rendering to absolute positions in the pdf. But what you need is an IElement you can add to the "document stack".
Try this (using full namespaces just to clearly identify the classes):
Barcode128 barcode = new Barcode128();
System.Drawing.Image img = barcode.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
iTextSharp.text.Image itextImg = iTextSharp.text.Image.GetInstance(img, System.Drawing.Imaging.ImageFormat.Bmp);
table.AddCell(new PdfPCell(itextImg));
I know that I can use system drawning method but image with bar code is not so clear.
Here is a code that I use. I'm trying to get documents whit different sizes depending on the number of lines in the document. So I'm using memory stream (not shown here). My idea is to write data to the table in memory and count lines. After that I will create new pdf document and add table to that document.
If I comment a line:
table.AddCell(image128);
both documents are the same. But if I leave that line in code final document shows all rows but text in rows is not visible. If I open second docu,ent in firefox I can even see a bar codes.
Any suggestion will be great. Or is there a different way to get documents with different sizes depending on amount of content.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing;
namespace testtables1
{
class Program
{
static void Main(string[] args)
{
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(300, 720);
Document pdfToCreate = new Document(pageSize, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(pdfToCreate, new FileStream(#"D:\\testfile.pdf", FileMode.Create));
pdfToCreate.Open();
PdfPTable table = new PdfPTable(1);
PdfPTable tableout = new PdfPTable(1);
WriteLineToPdf("This is first row. Below is image generated using SystemDrawing.", table, out tableout);
table = tableout;
// Create bar code
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.Code = "00100370006756555316";
// Create a new PdfWrite object, writing the output to a MemoryStream
var outputStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream);
PdfContentByte cb = new PdfContentByte(writer);
// Image generated using System.Drawing and rendering
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
iTextSharp.text.Image bmCoverted = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
table.AddCell(bmCoverted);
pdfToCreate.Open();
// Image generated using iTextSharp.text.Image
WriteLineToPdf("This is third row. Below is image generated using code128.CreateImageWithBarcode.", table, out tableout);
table = tableout;
iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
table.AddCell(image128);
table = tableout;
WriteLineToPdf("This is fifth row.", table, out tableout);
pdfToCreate.Add(tableout);
// Create new document with height that depends on number of lines in document.
iTextSharp.text.Rectangle psFinal = new iTextSharp.text.Rectangle(200, 300);
Document pdfFinal = new Document(psFinal, 0, 0, 0, 0);
PdfWriter writer1 = PdfWriter.GetInstance(pdfFinal, new FileStream(#"D:\\finalfile.pdf", FileMode.Create));
pdfFinal.Open();
pdfFinal.Add(tableout);
cb = null;
pdfToCreate.Close();
pdfFinal.Close();
}
// Write a single line to the PDF.
private static void WriteLineToPdf(string tLine, PdfPTable ticTable0In, out PdfPTable ticTableIN)
{
// Define fonts.
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); // Base font.
iTextSharp.text.Font timesN = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
Paragraph par = new Paragraph(tLine, timesN);
par.Alignment = Element.ALIGN_CENTER;
PdfPCell cell;
cell = new PdfPCell();
//cell.FixedHeight = 12f;
//cell.Border = Rectangle.NO_BORDER;
cell.AddElement(par);
ticTable0In.AddCell(cell);
ticTableIN = ticTable0In;
par = null;
}
}
}
My other ideai is to use data from memory stream. I'm able to create result.pdf document with size that I want but after I write Data from memory size changes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace TableWithBC2
{
class Program
{
static void Main(string[] args)
{
using (MemoryStream myMemoryStream = new MemoryStream())
{
PdfPTable tableIn;
Document pdfDocument = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdfDocument, myMemoryStream);
PdfPTable tableOut = new PdfPTable(new float[] { 200 });
tableIn = new PdfPTable(new float[] { 200 });
pdfDocument.Open();
for (int i = 0; i < 5; i++)
{
WriteLineToPdf("This is row no. " + i.ToString(), tableIn, out tableOut);
tableIn = tableOut;
}
// Create bar code
PdfContentByte cb = writer.DirectContent;
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128;
code128.ChecksumText = true;
code128.GenerateChecksum = true;
code128.Code = "00100370006756555316";
Image barCodeImage = code128.CreateImageWithBarcode(cb, null, null);
PdfPCell cell1 = new PdfPCell(barCodeImage);
tableOut.AddCell(cell1);
tableIn = tableOut;
WriteLineToPdf("This is a last row.", tableIn, out tableOut);
pdfDocument.Add(tableOut);
pdfDocument.Close();
// Create final PDF document.
Document pdfDocument1 = new Document(new Rectangle(200, 250));
PdfWriter writer1 = PdfWriter.GetInstance(pdfDocument1, new FileStream(#"D:\\result.pdf", FileMode.Create));
pdfDocument1.Open();
pdfDocument1.Add(new Chunk());
pdfDocument1.Close();
byte[] content = myMemoryStream.ToArray();
// Write out PDF from memory stream.
using (FileStream fs = File.OpenWrite(#"D:\\nikola bd - final\\out\\dummy22.pdf"))
{
fs.Write(content, 0, (int)content.Length);
}
}
}
// Write a single line to the PDF.
private static void WriteLineToPdf(string tLine, PdfPTable tTableIn, out PdfPTable tTableOut)
{
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font timesN = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.NORMAL, BaseColor.RED);
Paragraph par = new Paragraph(tLine, timesN);
par.Alignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell();
cell.AddElement(par);
tTableIn.AddCell(cell);
tTableOut = tTableIn;
}
}
}
This question already has answers here:
Convert TXT file to PDF using iText (keep formatting)
(3 answers)
Closed 6 years ago.
This is my code:
using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
{
//step 1
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 140f, 10f))
{
try
{
// step 2.
StreamReader sr = new StreamReader("TextFile.txt");
string line;
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);
pdfWriter.PageEvent = new ITextEvents();
//open the stream
pdfDoc.Open();
for (int i = 0; i < 100; i++)
{
if ((line = sr.ReadLine()) != null)
{
Paragraph para = new Paragraph(line, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 6));
para.Alignment = Element.ALIGN_LEFT;
pdfDoc.Add(para);
//pdfDoc.NewPage();
}
}
sr.Close();
pdfDoc.Close();
and it's working...It reads the text file that is in my computer and adds it to PDF... The problem is - it doesn't keep the formatting from the text file. It just adds text. And I need to keep it the way it is. Is there any way to keep the formatting of the text? Can I somehow add it to a table and format the table, or something like that?
Try this one
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace TxtToPdf
{
class Program
{
static void Main(string[] args)
{
//Read the Data from Input File
StreamReader rdr = new StreamReader("Path/Test.txt");
//Create a New instance on Document Class
Document doc = new Document();
//Create a New instance of PDFWriter Class for Output File
PdfWriter.GetInstance(doc, new FileStream("Path/Test.pdf", FileMode.Create));
//Open the Document
doc.Open();
//Add the content of Text File to PDF File
doc.Add(new Paragraph(rdr.ReadToEnd()));
//Close the Document
doc.Close();
//Open the Converted PDF File
System.Diagnostics.Process.Start("Path/Test.pdf");
}
}
}
You can do it by reading escape sequence too like when line changes you should Read it as \n and then write it in PDF with your text.
I have a simple block of code to create a pdf from an image using itextsharp. It works fine on it's own. However I wanted to convert the image to gray scale before copying it to the pdf. Since I'm already familiar with EMGU, I used that to convert the image. However, the instant EMGU touches the bitmap, itext sharp will only render a corner of the image rather than the whole thing (I've confirmed both in the debugger and by saving the new image back out that it is complete):
No EMGU:
Bitmap loaded to an EMGU Image:
Below is the code, with the 3 EMGU lines commented out (compiles to "good"):
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Structure;
using iTextSharp.text;
using iTextSharp.text.pdf;
public void Weird()
{
//Add the unmanaged open cv folder to the path (or copy the open cv components to the same dir as the exe)
if (!PathIsSet)
{
String path = Environment.GetEnvironmentVariable("PATH");
path = String.Join(";",
Path.Combine(Environment.CurrentDirectory, #"C:\OpenCV"),
path
);
Environment.SetEnvironmentVariable("Path", path);
PathIsSet = true;
}
var currDir = AppDomain.CurrentDomain.BaseDirectory;
var bmp = new Bitmap(Path.Combine(currDir, "sampleimage-good.png"));
//uncommenting these lines "corrupts" the image in a way where itextsharp only rights a small corner of it.
//var img2 = new Image<Bgra, int>(bmp);
//bmp = img2.ToBitmap();
//even saving and reopening doesn't work
//bmp.Save(Path.Combine(currDir, "sampleimage-good2.png"));
//bmp = new Bitmap(Path.Combine(currDir, "sampleimage-good2.png"));
var pdfPath = Path.Combine(currDir, "test.pdf");
using (var outputStream = new FileStream(pdfPath, FileMode.Create))
{
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(document, outputStream);
writer.CloseStream = false;
document.Open();
int pageCount = bmp.GetFrameCount(FrameDimension.Page);
PdfContentByte cb = writer.DirectContent;
for (int i = 0; i < pageCount; ++i)
{
bmp.SelectActiveFrame(FrameDimension.Page, i);
var img = iTextSharp.text.Image.GetInstance(bmp, System.Drawing.Imaging.ImageFormat.Jpeg);
// Scale the image to fit in the page
img.ScalePercent(72f / img.DpiX * 100);
img.SetAbsolutePosition(0, 0);
cb.AddImage(img);
}
document.Close();
}
Process.Start(pdfPath);
}
How can i set an image to a pdf field in existing pdf file?
I'm using the iTextSharp object.
Setting the text field is working fine. No problem in it.
pdfFormFields.SetField("Firstname", "Mujeeb");
Please help.
Remove the Text field and replace it with a Pushbutton field of the same size and position. If you set the Pushbutton to READ_ONLY then it can't be pressed and it will look like a static image. This keeps the image you're trying to add as a field annotation instead of adding it to the page content.
void ConvertTextFieldToImage(string inputFile, string fieldName, string imageFile, string outputFile)
{
using (PdfStamper stamper = new PdfStamper(new PdfReader(inputFile), File.Create(outputFile)))
{
AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions(fieldName)[0];
PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, fieldName);
imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
imageField.Image = iTextSharp.text.Image.GetInstance(imageFile);
imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
imageField.ProportionalIcon = false;
imageField.Options = BaseField.READ_ONLY;
stamper.AcroFields.RemoveField(fieldName);
stamper.AddAnnotation(imageField.Field, fieldPosition.page);
stamper.Close();
}
}
To the best of my knowledge you can't technically set a standard PDF field as an image (although you might be able to do this with XFA).
The workaround, however, is to just create a standard iTextSharp image and scale it to the form field's dimensions and place it where the field is.
Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.1.0 that shows how to do this. It starts by creating a very simple PDF with a single form field on it called "firstName". The second part of the program then gets the position and dimensions of that field and places an image there scaled appropriately. See the comments in the code for further details.
using System;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string baseFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "StartFile.pdf");
string secondFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SecondFile.pdf");
string TestImage = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.jpg");
//Create a very simple PDF with a single form field called "firstName"
using (FileStream fs = new FileStream(baseFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (Document doc = new Document(PageSize.LETTER))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
{
doc.Open();
writer.AddAnnotation(new TextField(writer, new iTextSharp.text.Rectangle(0, 0, 100, 100), "firstName").GetTextField());
doc.Close();
}
}
}
//Create a second file "filling out" the form above
using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(new PdfReader(baseFile), fs))
{
//GetFieldPositions returns an array of field positions if you are using 5.0 or greater
//This line does a lot and should really be broken up for null-checking
iTextSharp.text.Rectangle rect = stamper.AcroFields.GetFieldPositions("firstName")[0].position;
//Create an image
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(TestImage);
//Scale it
img.ScaleAbsolute(rect.Width, rect.Height);
//Position it
img.SetAbsolutePosition(rect.Left, rect.Bottom);
//Add it to page 1 of the document
stamper.GetOverContent(1).AddImage(img);
stamper.Close();
}
}
this.Close();
}
}
}
This is the answer that works for placing an image in a specific location. `
using (PdfStamper stamper = new PdfStamper(new PdfReader(fromFilePath), File.Create("toFilePath")))
{
AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions("btn1")[0];
PushbuttonField imageField = new PushbuttonField(stamper.Writer, fieldPosition.position, "btn1Replaced");
imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
imageField.Image = iTextSharp.text.Image.GetInstance(ImageLocationPath);
imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
imageField.ProportionalIcon = false;
imageField.Options = BaseField.READ_ONLY;
stamper.AcroFields.RemoveField("btn1");
stamper.AddAnnotation(imageField.Field, fieldPosition.page);
stamper.Close();
}
Hi I have a pdf I created using itextsharp.
Using pdfreader I am reading the created pdf into a pdfstamper.
Now I am trying to use the pdfstamper to add a black rectangle the size of the page on all pages. How do i do this?
Also I cannot use document to add the rectangle because the stream is close!
MemoryStream stream = new MemoryStream();
PdfReader pdfReader = new PdfReader(output.ToArray());
PdfStamper stamper = new PdfStamper(pdfReader, stream);
for (int x = 0; x < stamper.Reader.NumberOfPages; x++)
{
Rectangle rectangle = document.PageSize;
rectangle.BackgroundColor = new BaseColor(0, 0, 0);
//stamper.Writer.AcroForm.
//document.Add(rectangle);
}
output.Close();
pdfReader.Close();
stamper.Close();
If you want to draw using the PdfStamper then you need to use the PdfContentByte which you can get by calling stamper.GetOverContent(pageNum). There's a specific command on that object called Rectangle which does exactly what you want it to do. Also, remember that pages within a PDF start numbering at one and not zero.
Below is a full working C# 2010 WinForm app targeting iTextSharp 5.1.1.0 that should do what you're looking for, I think. You'll need to modify it to support the MemoryStream but that should be pretty easy.
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "input.pdf");
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.pdf");
PdfReader pdfReader = new PdfReader(inputFile);
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(pdfReader, fs))
{
int PageCount = pdfReader.NumberOfPages;
for (int x = 1; x <= PageCount; x++)
{
PdfContentByte cb = stamper.GetOverContent(x);
iTextSharp.text.Rectangle rectangle = pdfReader.GetPageSizeWithRotation(x);
rectangle.BackgroundColor = BaseColor.BLACK;
cb.Rectangle(rectangle);
}
}
}
this.Close();
}
}
}