Could not Remove Watermark from a PDF using iTextSharp - c#

I have been using iTextsharp to insert watermark in to a pdf & remove them. Thanks for the original posts:
Removing Watermark from a PDF using iTextSharp
Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I
I am able to add the watermark successfully as described in the above posts , however removing the watermark doesn't seem to work correctly. Below is the code block that actually removes the watermark from the PRStream.
if (content.IndexOf("/OC") >= 0 && content.IndexOf(watermarkText) >= 0) {
//Remove it by giving it zero length and zero data
stream.Put(PdfName.LENGTH, new PdfNumber(0));
stream.SetData(new byte[0]);
}
In my case, I have 8 pages with in the PDF with watermarks I've just added the above code to remove, which is being executed but the PdfReader is not getting updated in the PRStream.
Has anyone successfully removed a watermark layer using iTextSharp?

Related

Add MigraDoc Table of Contents to PDFsharp PDF

I am trying to use PDFsharp and MigraDoc to create a PDF report file from many other files.
Basically the workflow is as follows:
The user uploads 2 or more documents to concatenate
The user selects what pages they want to include in the TOC
The documents are concatenated together and a table of contents is generated
Now I am able to easily concatenate the documents and add bookmarks/outlines in PDFsharp. Where I have run into problems is when using MigraDoc to create the TOC referencing the PDFsharp created bookmarks/outlines as the targets of the hyperlinks.
Code to add bookmark/outline in PDFsharp (successfully adds the bookmarks):
// Current document is a PdfDocument and Title is the name of the bookmark
CurrentDocument.Outlines.Add(title, page, true, PdfOutlineStyle.Bold);
MigraDoc code to create the TOC page and render it to the current PDFsharp document:
// Create blank page
PdfPage page = (!hasTitlePage)
? AddPage(null, 0, TOC_BOOKMARK_TITLE) // Add to start
: AddPage(null, 1, TOC_BOOKMARK_TITLE); // Add after title page
// Get Graphics obj
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.MUH = PdfFontEncoding.Unicode;
// Create MigraDoc document + Setup styles
Document document = new Document();
DefineStyles(document);
// Add header
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph("Table of Contents");
paragraph.Format.Font.Size = 14;
paragraph.Format.Font.Bold = true;
paragraph.Format.SpaceAfter = 24;
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
// Add links - these are the PdfSharp outlines/bookmarks added previously when concatinating the pages
foreach (var bookmark in CurrentDocument.Outlines)
{
paragraph = section.AddParagraph();
paragraph.Style = "TOC";
Hyperlink hyperlink = paragraph.AddHyperlink(bookmark.Title);
hyperlink.AddText($"{bookmark.Title}\t");
hyperlink.AddPageRefField(bookmark.Title);
}
// Render document
DocumentRenderer docRenderer = new DocumentRenderer(document);
docRenderer.PrepareDocument();
docRenderer.RenderPage(gfx, 1);
return page;
Note - the MigraDoc code adds the TOC page but it doesn't recognize the PDFsharp bookmarks.
Screenshot of the TOC:
The TOC page is added at the start (although the TOC bookmark is listed at the end) but I cannot link to PDFsharp bookmarks
I am certain that the issue is with my understanding of MigraDoc and/or PDFsharp.
First prize would be to only use PDFsharp to add the TOC. I would appreciate any help in being pointed in the right direction.
MigraDoc is a world of its own - it uses PDFsharp to create PDF files, but cannot access internals of PDF files creates with PDFsharp (like Bookmarks in your case).
One approach: create the TOC with PDFsharp (as you already suggested).
Other approach: use MigraDoc to add pages from existing PDF files to a MigraDoc document to create the final PDF (you can add pages from PDF files like you add images).
With the second approach you can use the formatting capabilities of MigraDoc for the TOC and MigraDoc will insert the correct page numbers, provided you add MigraDoc Bookmarks for every image (imported PDF page).
With the first approach, you will have to do the formatting and insert the page numbers yourself, but you'll have more control over the final PDF.
Which is the "best" approach? Depends a bit on the extent of formatting you need for your TOC. With the second approach the TOC can have two or more pages and MigraDoc will take care of that automatically and entries in the TOC can have two or more lines and MigraDoc will also take care of that automatically. But I think a hack will be needed to add the Outlines (e.g. draw very small white text with the outline text somewhere on the page).
Update: For the PDFsharp only approach, you will add links with code like this:
PdfRectangle prect = new PdfRectangle(gfx.Transformer.WorldToDefaultPage(rect));
page.AddDocumentLink(prect, 1);
The second parameter to AddDocumentLink is the target page.

Ajax chart not rendering in Pdf Using pechkin asp.net c#

i have been using Pechkin (wkhtml wrapper for c#) to create pdf reports in my project and its working fine except i cant render any ajax chart(pie, bar, bubble or anything) in my created pdf, i have also tried rederdelay property but it didnt help me a bit.
the text data comes but the chart doesnt and the whole thing mess up. So i deduced that the problem is because created charts are not using standard html for the wrapper to handle, so in conclusion i need something which can convert these chart to images before html render on fly and it should be OpenSource/free.
thanks
Alok
P.S. I am using ajax control toolkit charts, pechkin is latest install from nuGet.
convert the chart into image and use this function to convert into byte array
private Byte[] Chart()
{
using (var chartimage = new MemoryStream())
{
chart.SaveImage(chartimage, ChartImageFormat.Png);
return chartimage.GetBuffer();
}
}
then create image from those byte[]
var image = Image.GetInstance(Chart());
image.ScalePercent(75f);
add this image in the document

C# Paste HTML to Excel or PowerPoint

How to paste HTML ( tables ) code into Excel or PowerPoint?
I've overcome some issues concerning pasting HTML into Excel and PowerPoint and noticed that a lot of people are asking that.
I'd like to share my research, solution I made out for it.
Let's say we have a html file named html and we would like to access it in Excel, let's do following:
Clipboard.SetText(html);
We copy our html into the Clipboard. The clipboard generates from the html a real table or image/chart from the input file.
System.Threading.Thread.Sleep(2000);
Let's wait a second to have a preview
sheet.Range(cellmapp).PasteSpecial();
Now, we paste the content into a range that we could like to paste it, by defining cellmap.
System.Threading.Thread.Sleep(1000);
Let's wait a second to see the output
sheet.UsedRange.Copy(Missing.Value);
Now, in order to copy the table image into PowerPoint, we must work the with UsedRange.Copy, because it will copy the currently selected Excel area.
In order to check that we paste it into the correct Powerpoint slide
foreach (PowerPoint.Slide slide in presentation.Slides)
{
foreach (PowerPoint.Shape pptshape in slide.Shapes)
{
if(<your condition satisfies>)
{
slide.Select(); // some position in any slide
pptshape.Delete();//delete old content that was in that slide
ppApp.ActiveWindow.View.PasteSpecial(); //paste the Excel content
}
}
}
Of course there are other solutions, like making an image out of the html code and pasting that, which was my initial idea.
Another post refering that manipulation:
Showing HTML in PowerPoint

setting Barcode Number using iTextSharp with PDF Form made in LiveCycle

Using asp.net with C# Codebehind and iTextSharp library.
I have a pdf form that I created in LiveCycle, that has text fields and a barcode (code 3 of 9). I use this template to create packing slips. When I run my code, I pull values out of the database and plug them into the text boxes and change the number value for the barcode. In order for the values to show up on the completed pdf, I have to flatten the pdf. It seems that when the pdf is flattened, I lose the barcode image. All that shows is the number that I set.
Does anyone have any Idea how to retain the barcode image when I flatten my pdf?
Here is a snippet of my code.
PdfReader pdfReader = new PdfReader(_pdfFullFilename);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(pdfTemplate, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
//...
foreach (string fieldKey in pdfFormFields.Fields.Keys)
{
if (fieldKey.Contains("BarCode[0]"))
pdfFormFields.SetField(fieldKey, _productNumber);
}
//...
pdfStamper.FormFlattening = true;
pdfStamper.Close();
pdfReader.Close();
Any Help would be much appreciated. Let me know if I need to expound on anything.

modifying a print preview

I'm building a c# app that displays a print preview (of a document) and then asks the user(s) to 'sign' the document via a InkPicture control. I've got no problems extracting the Bitmap from the inkpicture control and applying it to the PrintDocument (I do this earlier in the process before the print preview and paint those images to the printdocument) but the purpose of the print preview is to allow the user(s) to review the document as it would be printed and sign off on it.
I've tried resetting the document to a modified one
// MyDocumentType derives from PrintDocument and ipSignature is a
// user control derived from InkPicture that converts the ink to
// a gif
MyDocumentType doc = (MyDocumentType)ppcPreview.Document;
doc.AddSignature(ipSignature.Gif);
ppcPreview.Document = doc;
I've tried reconstructing the print preview control
MyDocumentType doc = (MyDocumentType)ppcPreview.Document;
doc.AddSignature(ipSignature.Gif);
ppcPreview = new PrintPreviewControl();
ppcPreview.Document = doc;
to no effect.
Invalidating the control after it's modified also does nothing.
I'm kind of stumped.
Did you try using the InvalidatePreview method instead of Invalidate?

Categories