itextsharp remove field position - itext

I have this code to get field positions
PdfReader reader = new PdfReader(ssPdf);
var output = new MemoryStream();
var stamper = new PdfStamper(reader, output);
IList<AcroFields.FieldPosition> positions =stamper.AcroFields.GetFieldPositions(ssName);
and this to remove one position rect
if (positions.count>0)
positions.RemoveAt(1);
But then i dont have any method to set the field positions list to the stamper.
If i call the GetFieldPositions again it will return all the fields and not without the one i deleted
IList<AcroFields.FieldPosition> positions2 = stamper.AcroFields.GetFieldPositions(ssName);
stamper.Close();
reader.Close();

Related

How to convert PowerPoint (.ppt) to PDF in Java

Only last slide is getting convert means last slide overlapping every slides. Can anyone suggest how to combine it in one PDF?
I have tried with different approach but they are first creating image and then PDF.
FileInputStream inputStream = new FileInputStream(in);
SlideShow ppt = new SlideShow(inputStream);
inputStream.close();
Dimension pgsize = ppt.getPageSize();
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(out));
document.setPageSize(new Rectangle(
(float)pgsize.getWidth(), (float)pgsize.getHeight()));
document.open();
PdfGraphics2D graphics= null;
for (int i = 0; i < ppt.getSlides().length; i++) {
Slide slide = ppt.getSlides()[i];
graphics = (PdfGraphics2D) pdfWriter.getDirectContent()
.createGraphics((float)pgsize.getWidth(), (float)pgsize.getHeight());
slide.draw(graphics);
}
graphics.dispose();
document.close();

Redaction using itext 5 for a scanned pdf

I was trying to redact data from scanned PDF using Itext 5 but somehow it's not working out for me. I was only able to create redaction but not able to apply it. I tried with their given examples but it only works until creating redaction
String fileName = "scanned.pdf";
String dest = "output_scannedPdf.pdf";
PdfReader reader = new PdfReader(SRC + fileName);
int page = 1;
Rectangle rect = new Rectangle(500, 50, 200, 300);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST + dest));
PdfAnnotation annotation = new PdfAnnotation(stamper.getWriter(), rect);
annotation.put(PdfName.SUBTYPE, new PdfName("Redact"));
annotation.setTitle("My author");
annotation.put(new PdfName("Subj"), new PdfName("Redact"));
float[] fillColor = { 0, 0, 0 };
annotation.put(new PdfName("IC"), new PdfArray(fillColor));
float[] fillColorRed = { 1, 0, 0 };
annotation.put(new PdfName("OC"), new PdfArray(fillColorRed));
stamper.addAnnotation(annotation, page);
// manipulatePdf(dest, dest1);
PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(stamper);
cleaner.cleanUp();
stamper.close();
reader.close();
I'm getting a null pointer exception in extractLocationsFromRedactAnnots() method PdfName annotSubtype = annotDict.getAsName(PdfName.SUBTYPE);

adding a textbox to the right corner of the existing pdf using ITextSharp in C#

I tiied to add a TextBox to the right corner of the existing pdf using c#, but im unable to get it done. I have wrote the following code,but it is not helping in solving the problem, can any body please suggest me
using (MemoryStream stream = new MemoryStream())
{
PdfReader reader = new PdfReader(bytes);
PdfReader.unethicalreading = true;
Paragraph p = new Paragraph();
Document doc = new Document();
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
PdfContentByte canvas = stamper.GetOverContent(1);
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
//PdfContentByte cb = null;
//PdfImportedPage page;
int pages = reader.NumberOfPages;
for (int i = 1; i <= pages; i++)
{
var size1 = reader.GetPageSize(i);
w = size1.Width;
h = size1.Height;
stamper.FormFlattening = true;
TextField tf = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(0, 0, 300, 100), displaytext);
//Change the orientation of the text
tf.Rotation = 0;
stamper.AddAnnotation(tf.GetTextField(), i);
}
}
bytes = stream.ToArray();
}
File.WriteAllBytes(str, bytes);
As the OP clarified in comments to the question, he wants
to add the text as a page content in the right bottom corner of the page and
the page content previously existing there to be removed.
A simple implementation of this would include
first covering the existing page content with a filled rectangle and
then writing text there.
These tasks can be achieved with these helper methods:
void EmptyTextBoxSimple(PdfStamper stamper, int pageNumber, Rectangle boxArea, BaseColor fillColor)
{
PdfContentByte canvas = stamper.GetOverContent(pageNumber);
canvas.SaveState();
canvas.SetColorFill(fillColor);
canvas.Rectangle(boxArea.Left, boxArea.Bottom, boxArea.Width, boxArea.Height);
canvas.Fill();
canvas.RestoreState();
}
and
ColumnText GenerateTextBox(PdfStamper stamper, int pageNumber, Rectangle boxArea)
{
PdfContentByte canvas = stamper.GetOverContent(pageNumber);
ColumnText columnText = new ColumnText(canvas);
columnText.SetSimpleColumn(boxArea);
return columnText;
}
E.g. like this:
using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
Rectangle cropBox = reader.GetCropBox(1);
Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
columnText.Go();
}
For this source page
the sample code generates this
Addendum
In a comment the OP indicated
it is working for all files but for some pdf files it is displaying in the middle
Eventually he supplied a sample file for which the issue occurs. And indeed, with this file the issue could be reproduced.
The cause for the issue is that the pages in the sample file use page rotation, something that iText (only) partially allows users to ignore. In particular iText automatically rotates text to be upright after rotation and transforms coordinates, but when retrieving the cropbox of a page, one still has to apply rotation before making use of it coordinates. Thus, a more complete example would be like this:
using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
{
Rectangle cropBox = reader.GetCropBox(1);
int rotation = reader.GetPageRotation(1);
while (rotation > 0)
{
cropBox = cropBox.Rotate();
rotation -= 90;
}
Rectangle bottomRight = new Rectangle(cropBox.GetRight(216), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(146));
EmptyTextBoxSimple(stamper, 1, bottomRight, BaseColor.WHITE);
ColumnText columnText = GenerateTextBox(stamper, 1, bottomRight);
columnText.AddText(new Phrase("Some test text to draw into a text box in the lower right corner of the first page"));
columnText.Go();
}

iText - Add text to existing landscape document

In my project, I am opening existing PDFs and add text on it at specific positions. This worked well since today where I got a landscape file. This is my code:
outputStream = new FileOutputStream(file + "_out.pdf");
PdfReader reader = new PdfReader(file);
PdfStamper stamper = new PdfStamper(reader, outputStream);
stamper.setRotateContents(true);
PdfContentByte canvas = stamper.getOverContent(1);
ColumnText.showTextAligned(canvas,
Element.ALIGN_LEFT, new Phrase("Text"), xPosition, yPosition, 0);
stamper.close();
reader.close();
outputStream.close();
When I opened the newly created file, the content is shown in portrait mode.
How do I have to change the code in order to get the file in landscape mode, together with the stamped text?
The file in question: http://www.share-online.biz/dl/RO1IPFVM0JX

Chapter-2 example on countrychunks

I am trying to run Countrychunks example from the Chapter 2. The example works but the line: document.Add(Chunk.NEWLINE); does not produce the new line and the loop overwrites the first line. I am posting my code here in case I am doing anything wrong:
public void createCountryChunks(String fileName)
{
iTextSharp.text.Font font;
Document document = new iTextSharp.text.Document();
//PdfWriter.GetInstance(document, new FileStream(fileName)).setInitialLeading(16);
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
font = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 6, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.WHITE);
foreach (var p in myProducts)
{
// add a country to the document as a Chunk
document.Add(new Chunk(p.pr_name));
document.Add(new Chunk(" "));
Chunk id = new Chunk(p.pr_URN.ToString(), font);
// with a background color
id.SetBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
// and a text rise
id.SetTextRise(6);
document.Add(id);
document.Add(Chunk.NEWLINE);
}
document.Close();
}
As you can see the example is a bit different because of the data but the rest is almost the same as the original Java example.
Any suggestions please?
The setInitialLeading call that you weren't able to bring over and was commented out was actually very important. Adding that back in will solve your problems. I really don't like to add properties directly onto my constructed objects so I'm going to do it in two lines:
var w = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
w.InitialLeading = 16;