Hiding a PDF text by adding an extra layer - c#

i need to hide a text by adding a new layer over the text i need to hide.
public void ReplacePDFText(string strSearch, StringComparison scCase, string strSource, string strDest)
{
PdfContentByte pCont = null;
if (File.Exists(strSource)) {
PdfReader pdfFileReader = new PdfReader(strSource);
using (PdfStamper psStamp = new PdfStamper(pdfFileReader, new FileStream(strDest, FileMode.Create))) {
for (int intCurrPage = 1; intCurrPage <= pdfFileReader.NumberOfPages; intCurrPage++) {
LocTextExtractionStrategy Strategy = new LocTextExtractionStrategy();
pCont = psStamp.GetUnderContent(intCurrPage);
Strategy.UndercontentCharacterSpacing = pCont.CharacterSpacing;
Strategy.UndercontentHorizontalScaling = pCont.HorizontalScaling;
string currText = PdfTextExtractor.GetTextFromPage(pdfFileReader, intCurrPage, Strategy);
List<iTextSharp.text.Rectangle> lstMatches = Strategy.GetTextLocations(strSearch, scCase);
PdfLayer pdLayer = default(PdfLayer);
pdLayer = new PdfLayer("over", psStamp.Writer);
pCont.SetColorFill(BaseColor.BLACK);
foreach (Rectangle rctRect in lstMatches) {
pCont.Rectangle(rctRect.Left, rctRect.Bottom, rctRect.Width, rctRect.Height);
pCont.Fill();
}
}
}
pdfFileReader.Close();
}
}
The problem with the approach above, is that the layer is added successfully with black color. So instead of the text i have a beautiful black line over the text.
But if i set the pCont.SetColorFill(BaseColor.BLACK) to WHITE, the text is still displayed.
How can i overcome this issue?

Instead of:
pCont = psStamp.GetUnderContent(intCurrPage);
Use:
pCont = psStamp.GetOverContent(intCurrPage);

Related

Expander Control - Read txt-Files in C# (WindowsForms)

I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.
public Form1()
{
InitializeComponent();
CreateAccordion();
}
private void CreateAccordion(string book1)
{
Accordion accordion = new Accordion();
accordion.Size = new Size(400, 350);
accordion.Left = 10;
Expander expander1 = new Expander();
expander1.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
CreateContentLabel(expander1, book1, 140);
accordion.Add(expander1);
Expander expander2 = new Expander();
expander2.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
CreateContentLabel(expander2, "book2", 120);
accordion.Add(expander2);
this.Controls.Add(accordion);
}
The following code reads only a single txt.file. Can anyone help me please?
private void CreateAccordion()
{
ReadTxtFiles();
}
private void ReadTxtFiles()
{
string path = #"C:\..\..\READBOOKS";
string[] files = new string[] { "book1.txt", "book2.txt" };
foreach (string file in files)
{
string fullPath = Path.Combine(path, file);
string booktxt = File.ReadAllText(fullPath);
string book1 = booktxt;
CreateAccordion(book1);
}
}
I‘m trying to add the second .txt file (book2) into Text Expander control, unfortunately without success.
The main reason behind this is because you're only passing the string from your first book, you'll need to pass all of the text.
private void ReadTxtFiles()
{
string path = #"C:\..\..\READBOOKS";
string[] files = new string[] { "book1.txt", "book2.txt" };
List<string> books = new List<string>();
foreach (string file in files)
{
string fullPath = Path.Combine(path, file);
string booktxt = File.ReadAllText(fullPath);
books.Add(booktxt);
}
CreateAccordion(books);
}
Change the signature of CreateAccordion:
private void CreateAccordion(List<string) books)
{
Accordion accordion = new Accordion();
accordion.Size = new Size(400, 350);
accordion.Left = 10;
Expander expander1 = new Expander();
expander1.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander1, "Book1", SystemColors.ActiveBorder);
CreateContentLabel(expander1, books[0], 140);
accordion.Add(expander1);
Expander expander2 = new Expander();
expander2.BorderStyle = BorderStyle.FixedSingle;
ExpanderHelper.CreateLabelHeader(expander2, "Book2", SystemColors.ActiveBorder);
CreateContentLabel(expander2, books[1], 120);
accordion.Add(expander2);
this.Controls.Add(accordion);
}
Please note that I'm accessing index's here, you may want to check that the index exist before trying to access it. Also there's more way's than this, but should give you a better understanding to accomplish this.

c# How to cast from 'iTextSharp.text.pdf.PdfArray' to 'iTextSharp.text.pdf.PRIndirectReference'

I was using this piece of code till today and it was working fine:
for (int page = 1; page <= reader.NumberOfPages; page++)
{
var cpage = reader.GetPageN(page);
var content = cpage.Get(PdfName.CONTENTS);
var ir = (PRIndirectReference)content;
var value = reader.GetPdfObject(ir.Number);
if (value.IsStream())
{
PRStream stream = (PRStream)value;
var streamBytes = PdfReader.GetStreamBytes(stream);
var tokenizer = new PRTokeniser(new RandomAccessFileOrArray(streamBytes));
try
{
while (tokenizer.NextToken())
{
if (tokenizer.TokenType == PRTokeniser.TK_STRING)
{
string strs = tokenizer.StringValue;
if (!(br = excludeList.Any(st => strs.Contains(st))))
{
//strfor += tokenizer.StringValue;
if (!string.IsNullOrWhiteSpace(strs) &&
!stringsList.Any(i => i == strs && excludeHeaders.Contains(strs)))
stringsList.Add(strs);
}
}
}
}
finally
{
tokenizer.Close();
}
}
}
But today I got an exception for some pdf file: Unable to cast object of type 'iTextSharp.text.pdf.PdfArray' to type 'iTextSharp.text.pdf.PRIndirectReference
On debugging I got to know that the error is at this line: var ir = (PRIndirectReference)content;. That's because the pdf content that I'm extracting, I get it in the form of ArrayList, as you can see from the below image:
It would be really grateful if anyone can help me with this. Thanks in advance.
EDIT :
The pdf contents are paragraphs, tables, headers & footers, images in few cases. But I'm not bothered of images as I'm bypassing them.
As you can see from the code I'm trying to add the words into a string list, so I expect the output as plain text; words to be specific.
That was real easy! Don't know why I couldn't make out.
PdfReader reader = new PdfReader(name);
List<string> stringsList = new List<string>();
for (int page = 1; page <= reader.NumberOfPages; page++)
{
//directly get the contents into a byte stream
var streamByte = reader.GetPageContent(page);
var tokenizer = new PRTokeniser(new RandomAccessFileOrArray(streamByte));
var sb = new StringBuilder(); //use a string builder instead
try
{
while (tokenizer.NextToken())
{
if (tokenizer.TokenType == PRTokeniser.TK_STRING)
{
var currentText = tokenizer.StringValue;
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
sb.Append(tokenizer.StringValue);
}
}
}
finally
{
//add appended strings into a string list
if(sb != null)
stringsList.Add(sb.ToString());
tokenizer.Close();
}
}

iTextSharp: Setting text on textfield that is on a table cell results in text being too wide and squashed

I think this might be a bug, but if anyone can help I'd appreciate it. I currently have another question open that deals with a similar issue, but I think this question better exemplifies the problem, and more simply too. That being said I don't want to delete the old one in case it increases my wait time. I yield to the mods to decide which question is better.
Here's a sample application that creates a pdf, then a table. It adds a cell to the table and then ties a fieldpositioningevent to the cell event.
using System;
using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextSharpTextBoxInTableCell
{
class Program
{
static void Main(string[] args)
{
// Create a PDF with a TextBox in a table cell
BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
Font helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, BaseColor.BLACK);
Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
FileStream fs = new FileStream("TextBoxInTableCell.pdf", FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfPTable myTable = new PdfPTable(1);
myTable.TotalWidth = 568f;
myTable.LockedWidth = true;
myTable.HorizontalAlignment = 0;
TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
tf.Text = "test";
PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
iTextSharp.text.pdf.events.FieldPositioningEvents events =
new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events;
myTable.AddCell(tbCell);
doc.Add(myTable);
doc.Close();
fs.Close();
Process.Start("TextBoxInTableCell.pdf");
Console.WriteLine("End Of Program Execution");
Console.ReadLine();
}
}
}
Here's what this field looks like when it's generated:
As you can see, the text is squashed. I've published the generated pdf here.
I'm definitely seeing what you're seeing and as #mkl said in your other post, the problem comes down to the appearance's BBOX entry not being set to the same size as the field. I can't really find too many examples of FieldPositioningEvents in the wild and the ones that do exist appear to be copy-and-paste's of each other for the most part.
Anyway, if you read the actual code for FieldPositioningEvents you'll see that it can be used for both page events as well as cell events which makes me think it was intended for broader purposes possibly, but that's just a guess on my part.
One solution is to just write your own subclass of IPdfPCellEvent. Below is an example of that that follows the example provided by FieldPositioningEvents however it is specific to TextFields since we're interested in setting the /BBOX entry. It has two constructors, one that works very similar to FieldPositioningEvents that takes a PdfWriter and a TextField and one that just takes the most commonly set properties of a TextFields and actually creates it for you. The CellLayout is part of the interface contract and actually figures out where the annotation should be drawn.
public class SingleCellFieldPositioningEvent : IPdfPCellEvent {
public TextField Field { get; set; }
public PdfWriter Writer { get; set; }
public float Padding { get; set; }
public SingleCellFieldPositioningEvent(PdfWriter writer, TextField field) {
this.Field = field;
this.Writer = writer;
}
public SingleCellFieldPositioningEvent(PdfWriter writer, string fieldName, string text = "", BaseFont font = null, float fontSize = 14 ) {
//The rectangle gets changed later so it doesn't matter what we use
var rect = new iTextSharp.text.Rectangle(1, 1);
//Create the field and set various properties
this.Field = new TextField(writer, rect, fieldName);
this.Field.Text = text;
if (null == font) {
font = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
}
this.Field.Font = font;
this.Field.FontSize = fontSize;
this.Writer = writer;
}
public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvases) {
//Create the field's rectangle based on the current cell and requested padded
var newRect = new PdfRectangle(rect.GetLeft(Padding), rect.GetBottom(Padding), rect.GetRight(Padding), rect.GetTop(Padding));
//Set the appearance's rectangle to the same as the box
Field.Box = newRect.Rectangle;
//Get the raw field
var tf = this.Field.GetTextField();
//Change the field's rectangle
tf.Put(PdfName.RECT, newRect);
//Add the annotation to the writer
Writer.AddAnnotation(tf);
}
}
You can use this in two different ways. Either manually create a field and set various properties:
//The rectangle is actually changed in the cell event so it doesn't matter what we use
TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(1, 1), "cellTextBox");
tf.Text = "test";
tf.Font = bfHelvetica;
tf.FontSize = 14;
PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
tbCell.CellEvent = new SingleCellFieldPositioningEvent(writer, tf);
Or just pass the properties in:
PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
tbCell.CellEvent = new SingleCellFieldPositioningEvent(writer, "cellTextBox", "test", bfHelvetica, 14);
myTable.AddCell(tbCell);

highlighting text in Docx using c#

I need to highlight a sentence in docx file, I have this code, and its working fine for many documents , but i noticed that for some document the text inside the document is set word by word, not whole sentence, I mean each word with its own Run, so when searching for that sentence, it is not found because it is word by word in the docx.
NOTE: I am working with Arabic text.
private void HighLightText_userSentence(Paragraph paragraph, string text, string title, string author, decimal percentage, string _color)
{
string textOfRun = string.Empty;
var runCollection = paragraph.Descendants<Run>();
Run runAfter = null;
//find the run part which contains the characters
foreach (Run run in runCollection)
{
if (run.GetFirstChild<Text>() != null)
{
textOfRun = run.GetFirstChild<Text>().Text.Trim();
if (textOfRun.Contains(text))
{
//remove the character from thsi run part
run.GetFirstChild<Text>().Text = textOfRun.Replace(text, "");
runAfter = run;
break;
}
}
}
// create a new run with your customization font and the character as its text
Run HighLightRun = new Run();
RunProperties runPro = new RunProperties();
RunFonts runFont = new RunFonts() { Ascii = "Curlz MT", HighAnsi = "Curlz MT" };
Bold bold = new Bold();
DocumentFormat.OpenXml.Wordprocessing.Color color = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = _color };
DocumentFormat.OpenXml.Wordprocessing.FontSize fontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize() { Val = "22" };
FontSizeComplexScript fontSizeComplex = new FontSizeComplexScript() { Val = "24" };
Text runText = new Text() { Text = text };
//runPro.Append(runFont);
runPro.Append(bold);
runPro.Append(color);
//runPro.Append(fontSize);
// runPro.Append(fontSizeComplex);
HighLightRun.Append(runPro);
HighLightRun.Append(runText);
//HighLightRun.AppendChild(new Break());
//HighLightRun.PrependChild(new Break());
//insert the new created run part
paragraph.InsertBefore(HighLightRun, runAfter);
}
I recently used docX and was facing problems with searching and higlighting text. I tried an indirect way. It simple and works in most situations. I do it using the replace statement.
here search text is the text you want to highlight
using (DocX doc = DocX.Load("d:\\Sample.docx"))
{
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
foreach (var item in doc.Paragraphs[i])
{
if (doc.Paragraphs[i] is Paragraph)
{
Paragraph sen = doc.Paragraphs[i] as Paragraph;
Formatting form = new Formatting();
form.Highlight = Highlight.yellow;
form.Bold = true;
sen.ReplaceText(searchText, searchText, false,
System.Text.RegularExpressions.RegexOptions.IgnoreCase,
form, null, MatchFormattingOptions.ExactMatch);
}
}
}
doc.Save();
}

how can I change the font open xml

How can I change the font family of the document via OpenXml ?
I tried some ways but, when I open the document, it's always in Calibri
Follow my code, and what I tried.
The Header Builder I think is useless to post
private static void BuildDocument(string fileName, List<string> lista, string tipo)
{
using (var w = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
var mp = w.AddMainDocumentPart();
var d = new DocumentFormat.OpenXml.Wordprocessing.Document();
var b = new Body();
var p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
var r = new Run();
// Get and format the text.
for (int i = 0; i < lista.Count; i++)
{
Text t = new Text();
t.Text = lista[i];
if (t.Text == " ")
{
r.Append(new CarriageReturn());
}
else
{
r.Append(t);
r.Append(new CarriageReturn());
}
}
// What I tried
var rPr = new RunProperties(new RunFonts() { Ascii = "Arial" });
lista.Clear();
p.Append(r);
b.Append(p);
var hp = mp.AddNewPart<HeaderPart>();
string headerRelationshipID = mp.GetIdOfPart(hp);
var sectPr = new SectionProperties();
var headerReference = new HeaderReference();
headerReference.Id = headerRelationshipID;
headerReference.Type = HeaderFooterValues.Default;
sectPr.Append(headerReference);
b.Append(sectPr);
d.Append(b);
// Customize the header.
if (tipo == "alugar")
{
hp.Header = BuildHeader(hp, "Anúncio Aluguel de Imóvel");
}
else if (tipo == "vender")
{
hp.Header = BuildHeader(hp, "Anúncio Venda de Imóvel");
}
else
{
hp.Header = BuildHeader(hp, "Aluguel/Venda de Imóvel");
}
hp.Header.Save();
mp.Document = d;
mp.Document.Save();
w.Close();
}
}
In order to style your text with a specific font follow the steps listed below:
Create an instance of the RunProperties class.
Create an instance of the RunFont class. Set the Ascii property to the desired font familiy.
Specify the size of your font (half-point font size) using the FontSize class.
Prepend the RunProperties instance to your run containing the text to style.
Here is a small code example illustrating the steps described above:
private static void BuildDocument(string fileName, List<string> text)
{
using (var wordDoc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
var mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document = new Document();
var run = new Run();
foreach (string currText in text)
{
run.AppendChild(new Text(currText));
run.AppendChild(new CarriageReturn());
}
var paragraph = new Paragraph(run);
var body = new Body(paragraph);
mainPart.Document.Append(body);
var runProp = new RunProperties();
var runFont = new RunFonts { Ascii = "Arial" };
// 48 half-point font size
var size = new FontSize { Val = new StringValue("48") };
runProp.Append(runFont);
runProp.Append(size);
run.PrependChild(runProp);
mainPart.Document.Save();
wordDoc.Close();
}
}
Hope, this helps.
If you are using Stylesheet just add an instance of FontName property at appropriate font index during Fonts initilaization.
private Stylesheet GenerateStylesheet()
{
Stylesheet styleSheet = null;
Fonts fonts = new Fonts(
new Font( // Index 0 - default
new FontSize() { Val = 8 },
new FontName() { Val = "Arial"} //i.e. or any other font name as string
);
Fills fills = new Fills( new Fill(new PatternFill() { PatternType = PatternValues.None }));
Borders borders = new Borders( new Border() );
CellFormats cellFormats = new CellFormats( new CellFormat () );
styleSheet = new Stylesheet(fonts, fills, borders, cellFormats);
return styleSheet;
}
Then use it in Workbook style part as below.
WorkbookStylesPart stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
stylePart.Stylesheet = GenerateStylesheet();
stylePart.Stylesheet.Save();

Categories

Resources