使用 itextsharp 在浏览器中不显示在 pdf 中添加的文本
Added text in pdf not displayed in browser using itextsharp
我添加了 pdf 格式的文本。但是添加的文本没有显示在浏览器中。
using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
for (int x = 1; x < reader.NumberOfPages; x++)
{
cb = stamper.GetUnderContent(x);
myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
strategy.UndercontentCharacterSpacing = cb.CharacterSpacing;
strategy.UndercontentHorizontalScaling = cb.HorizontalScaling;
string currentText = PdfTextExtractor.GetTextFromPage(reader, x, strategy);
foreach (iTextSharp.text.Rectangle rect in MatchesFound)
{
//----------Annotation ----------------
iTextSharp.text.Rectangle rect_txt = rect;
BaseFont helv = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
rect_txt.Bottom = rect_txt.Top - 2;
rect_txt.Top = rect_txt.Top + 8;
rect_txt.Right = rect_txt.Right - ((rect_txt.Width) - Convert.ToInt32(text_num.ToString().Length) * 5);
rect_txt.Left = rect_txt.Left - 3.0f;
PdfAnnotation comment = PdfAnnotation.CreateFreeText(stamper.Writer, rect_txt, text_num.ToString(), cb);
comment.PlaceInPage = x;
comment.Put(PdfName.RC, new PdfString("<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:11.0.13\" xfa:spec=\"2.0.2\" style=\"font-size:9.0pt;text-align:left;color:#FF0000;font-weight:bold;font-style:normal;font-family:'helvetica';font-stretch:normal\"><p dir=\"ltr\"><span style=\"font-family:helvetica\">" + text_num.ToString() + "</span></p></body>", PdfObject.TEXT_UNICODE));
comment.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
stamper.AddAnnotation(comment, x);
}
}
}
}
//----------Annotation --------------
iTextSharp.text.Rectangle rect_txt = rect;
rect_txt.Bottom = rect_txt.Top - 2;
rect_txt.Top = rect_txt.Top + 8;
rect_txt.Right = rect_txt.Right - ((rect_txt.Width) - Convert.ToInt32(text_num.ToString().Length) * 5);
rect_txt.Left = rect_txt.Left - 3.0f;
BaseFont helv = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
appearance = PdfAppearance.CreateAppearance(stamper.Writer, rect_txt.Width, rect_txt.Height);
appearance.Rectangle(0, 0, rect_txt.Width, rect_txt.Height);
appearance.SetFontAndSize(helv, 12);
appearance.SetColorFill(BaseColor.RED);
ColumnText.ShowTextAligned(appearance, Element.ALIGN_LEFT, new Phrase(text_num), 0, 0, 0);
//appearance.FillStroke();
// appearance.ShowTextAligned(Element.ALIGN_LEFT, text_num.ToString(), 0, 0, 0);
PdfAnnotation comment = PdfAnnotation.CreateFreeText(stamper.Writer, rect_txt, text_num.ToString(), cb);
comment.PlaceInPage = x;
comment.Put(PdfName.RC, new PdfString("<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:11.0.13\" xfa:spec=\"2.0.2\" style=\"font-size:10px;text-align:left;color:#FF0000;font-weight:bold;font-style:normal;font-family:'helvetica';font-stretch:normal\"><p dir=\"ltr\"><span style=\"font-family:helvetica\">" + text_num.ToString() + "</span></p></body>", PdfObject.TEXT_UNICODE));
comment.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
comment.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance);
stamper.AddAnnotation(comment, x);
我添加了 pdf 格式的文本。但是添加的文本没有显示在浏览器中。
using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
for (int x = 1; x < reader.NumberOfPages; x++)
{
cb = stamper.GetUnderContent(x);
myLocationTextExtractionStrategy strategy = new myLocationTextExtractionStrategy();
strategy.UndercontentCharacterSpacing = cb.CharacterSpacing;
strategy.UndercontentHorizontalScaling = cb.HorizontalScaling;
string currentText = PdfTextExtractor.GetTextFromPage(reader, x, strategy);
foreach (iTextSharp.text.Rectangle rect in MatchesFound)
{
//----------Annotation ----------------
iTextSharp.text.Rectangle rect_txt = rect;
BaseFont helv = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
rect_txt.Bottom = rect_txt.Top - 2;
rect_txt.Top = rect_txt.Top + 8;
rect_txt.Right = rect_txt.Right - ((rect_txt.Width) - Convert.ToInt32(text_num.ToString().Length) * 5);
rect_txt.Left = rect_txt.Left - 3.0f;
PdfAnnotation comment = PdfAnnotation.CreateFreeText(stamper.Writer, rect_txt, text_num.ToString(), cb);
comment.PlaceInPage = x;
comment.Put(PdfName.RC, new PdfString("<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:11.0.13\" xfa:spec=\"2.0.2\" style=\"font-size:9.0pt;text-align:left;color:#FF0000;font-weight:bold;font-style:normal;font-family:'helvetica';font-stretch:normal\"><p dir=\"ltr\"><span style=\"font-family:helvetica\">" + text_num.ToString() + "</span></p></body>", PdfObject.TEXT_UNICODE));
comment.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
stamper.AddAnnotation(comment, x);
}
}
}
}
//----------Annotation --------------
iTextSharp.text.Rectangle rect_txt = rect;
rect_txt.Bottom = rect_txt.Top - 2;
rect_txt.Top = rect_txt.Top + 8;
rect_txt.Right = rect_txt.Right - ((rect_txt.Width) - Convert.ToInt32(text_num.ToString().Length) * 5);
rect_txt.Left = rect_txt.Left - 3.0f;
BaseFont helv = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
appearance = PdfAppearance.CreateAppearance(stamper.Writer, rect_txt.Width, rect_txt.Height);
appearance.Rectangle(0, 0, rect_txt.Width, rect_txt.Height);
appearance.SetFontAndSize(helv, 12);
appearance.SetColorFill(BaseColor.RED);
ColumnText.ShowTextAligned(appearance, Element.ALIGN_LEFT, new Phrase(text_num), 0, 0, 0);
//appearance.FillStroke();
// appearance.ShowTextAligned(Element.ALIGN_LEFT, text_num.ToString(), 0, 0, 0);
PdfAnnotation comment = PdfAnnotation.CreateFreeText(stamper.Writer, rect_txt, text_num.ToString(), cb);
comment.PlaceInPage = x;
comment.Put(PdfName.RC, new PdfString("<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:APIVersion=\"Acrobat:11.0.13\" xfa:spec=\"2.0.2\" style=\"font-size:10px;text-align:left;color:#FF0000;font-weight:bold;font-style:normal;font-family:'helvetica';font-stretch:normal\"><p dir=\"ltr\"><span style=\"font-family:helvetica\">" + text_num.ToString() + "</span></p></body>", PdfObject.TEXT_UNICODE));
comment.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);
comment.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance);
stamper.AddAnnotation(comment, x);