Itextsharp: Is There Any Way To Copy Links When Import Pages? - asp.net

I'm working on a project where i need to edit pdf's before display it
I need
add a watermark
edit permissions ( lock for avoid 'copy/paste' and 'save as' )
edit viewer preferences
And i did it... and work fine except for one thing, the links in the original file does not work in the new file... any idea?
NOTE: Actually, this is my code ( i'm using itextsharp )
private void loadPdf()
{
if (Request.QueryString.HasKeys())
{
if (Request.QueryString.GetKey(0) == "thepath" && Request.QueryString.GetKey(1) == "isprintable" && Request.QueryString.GetKey(2) == "type")
{
#region kuak
Document doc = new Document();
PdfReader pdfReader = new PdfReader(Request.QueryString["thepath"]);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
pdfWriter.ViewerPreferences = PdfWriter.PageModeUseOutlines;
//pdfWriter.ViewerPreferences = PdfWriter.PageLayoutTwoColumnLeft; /// Despliega el docuemnto en pares de hojas
pdfWriter.ViewerPreferences = PdfWriter.PageLayoutOneColumn;
pdfWriter.ViewerPreferences = PdfWriter.HideToolbar;
//pdfWriter.ViewerPreferences = PdfWriter.HideWindowUI; /// quita los scrollbars y el panel de la derecha qur contiene los bookmarks y las buskedas dentro del pdf
if (Request.QueryString["isprintable"] == "n")
{
pdfWriter.ViewerPreferences = PdfWriter.HideMenubar;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
pdfWriter.SetEncryption(null, encoding.GetBytes("mYpAssss"), 0, PdfWriter.STRENGTH40BITS);
}
doc.Open();
PdfContentByte pdfContentByte = pdfWriter.DirectContent;
doc.AddDocListener(pdfWriter);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
//doc.SetPageSize(pdfReader.GetPageSize(page));
doc.SetPageSize(pdfReader.GetPageSizeWithRotation(page));
doc.NewPage();
PdfImportedPage pdfImportedPage = pdfWriter.GetImportedPage(pdfReader, page);
int rot = pdfReader.GetPageRotation(page);
if (rot == 90 || rot == 270)
pdfContentByte.AddTemplate(pdfImportedPage, 0, -1.0F, 1.0F, 0, 0, pdfReader.GetPageSizeWithRotation(page).Height);
else
pdfContentByte.AddTemplate(pdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0);
string theId = findId();
if (isWatermarkNeeded(theId))
{
#region ADD TEXT WATERMARK
//pdfContentByte.BeginText();
//iTextSharp.text.Rectangle pageSize = pdfReader.GetPageSizeWithRotation(page);
//BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, System.Text.Encoding.ASCII.EncodingName, false);
//pdfContentByte.SetFontAndSize(baseFont, 200);
//BaseColor baseColor = new BaseColor(255, 0, 0, 20);
//pdfContentByte.SetColorFill(baseColor);
//float textAngle = (float)GetHypotenuseAngleInDegreesFrom(pageSize.Height, pageSize.Width);
//pdfContentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "DRAFT", 350, pageSize.Height / 2, textAngle);
//pdfContentByte.EndText();
#endregion
#region ADD IMAGE WATERMARK
string fechaExp = "Este documento vence: " + GetExpirationDate(theId).ToShortDateString();
pdfContentByte.BeginText();
//iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/images/watermark3.png"));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(ImageCheck.CreatePicture(#"C:\Users\myUser\Desktop\watermark.png", fechaExp).ToArray());
img.SetAbsolutePosition(0, 0);
pdfContentByte.AddImage(img);
pdfContentByte.EndText();
#endregion
}
}
pdfReader.Close();
doc.Close();
byte[] content = memoryStream.ToArray();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", content.Length.ToString());
Response.BinaryWrite(content);
}
#endregion
}
else
{
//hay querystring pro no corresponden con los que se necesita
}
}
else
{
//no se enviaron los querystring
}
}

You have to get the links from original PDF
var links = reader.GetLinks(pageNumber);
And write them to the new PDF
foreach (var link in links)
{
var annotation = link.CreateAnnotation(pdfWriter);
writer.AddAnnotation(annotation);
}

Related

while export to pdf using itexsharp it shows blank page

i made one desktop application using asp.net which convert html to PDF.
basically this application used for generate users PDF from database.
process: first of all i am converting all data to html and then convert to PDF using itextsharp but after generating some PDFs it shows blank pages
any idea or anyone face this type of issue
public static void converttopdf(string HtmlStream, List<Tuple<string, string>> tifffiles, List<Tuple<string, string>> pdffilestomerge, string filename, string patientfirstpagestr, string TableofContent, string patientheader, SqlConnection con, string sectiondetails)
{
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(HtmlStream);
Document document = new Document(PageSize.A4, 30, 30, 42, 44);
string filetogenerate = string.Empty;
if (pdffilestomerge != null && pdffilestomerge.Count > 1)
{
filetogenerate = temppath + filename + "_Temp1.pdf";
}
else
{
filetogenerate = temppath + filename + "_Temp.pdf";
}
PdfWriter writer = PdfWriter.GetInstance(document, new System.IO.FileStream(filetogenerate, System.IO.FileMode.Create));
HTMLWorker worker = new HTMLWorker(document);
document.Open();
worker.StartDocument();
string[] separator = new string[] { #"<br clear='all' style='page-break-before:always'>" };
string[] pages = HtmlStream.Split(separator, StringSplitOptions.None);
foreach (string page in pages)
{
document.NewPage();
System.Collections.ArrayList htmlarraylist = HTMLWorker.ParseToList(new StringReader(page), null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
document.Add((IElement)htmlarraylist[k]);
}
}
using (var ms = new MemoryStream())
{
if (tifffiles != null)
{
int docid = 0;
foreach (var obj in tifffiles)
{
string filepath = obj.Item2.ToString();
WriteLogEntry("bitmap file path : " + filepath);
if (filepath != string.Empty)
{
try
{
Bitmap myBitmap = new Bitmap(filepath);
System.Drawing.Color pixelColor = myBitmap.GetPixel(50, 50);
if (pixelColor.Name == "ff808080")
{
WriteLogEntry("convert image by irfanview :" + filepath);
LaunchCommandLineApp(filepath, temppath + "Test.jpg");
document.NewPage();
var imgStream = GetImageStream(temppath + "Test.jpg");
var image = iTextSharp.text.Image.GetInstance(imgStream);
image.SetAbsolutePosition(10, 80);
image.ScaleToFit(document.PageSize.Width - 60, document.PageSize.Height - 80);
//image.ScaleToFit(document.PageSize.Width - 30, document.PageSize.Height);
if (docid != Convert.ToInt32(obj.Item1))
{
Chunk c1 = new Chunk("#~#DID" + obj.Item1.ToString() + "#~#");
c1.Font.SetColor(0, 0, 0); //#00FFFFFF
c1.Font.Size = 0;
document.Add(c1);
}
document.Add(image);
File.Delete(temppath + "Test.jpg");
}
else
{
document.NewPage();
var imgStream = GetImageStream(filepath);
var image = iTextSharp.text.Image.GetInstance(imgStream);
image.SetAbsolutePosition(10, 80);
image.ScaleToFit(document.PageSize.Width - 60, document.PageSize.Height - 80);
//image.ScaleToFit(document.PageSize.Width - 30, document.PageSize.Height);
if (docid != Convert.ToInt32(obj.Item1))
{
Chunk c1 = new Chunk("#~#DID" + obj.Item1.ToString() + "#~#");
c1.Font.SetColor(0, 0, 0); //#00FFFFFF
c1.Font.Size = 0;
document.Add(c1);
}
document.Add(image);
WriteLogEntry("Image added successfully" + filepath);
}
}
catch
{
document.NewPage();
if (docid != Convert.ToInt32(obj.Item1))
{
Chunk c1 = new Chunk("#~#DID" + obj.Item1.ToString() + "#~#");
c1.Font.SetColor(0, 0, 0); //#00FFFFFF
c1.Font.Size = 0;
document.Add(c1);
}
WriteLogEntry("Image not valid" + filepath);
}
docid = Convert.ToInt32(obj.Item1);
}
}
}
}
worker.EndDocument();
worker.Close();
document.Close();
if (pdffilestomerge != null && pdffilestomerge.Count > 1)
{
string file = temppath + filename + "_Temp.pdf";
mergepdf(file, pdffilestomerge, filetogenerate);
}
PdfReader pdfreader = new PdfReader(temppath + filename + "_Temp.pdf");
Document document1 = new Document(PageSize.A4, 30, 30, 42, 44);
PdfWriter writer1 = PdfWriter.GetInstance(document1, new FileStream(FinalOutputPath + filename + ".pdf", FileMode.Create));
//HeaderFooter footer = new HeaderFooter(new Phrase("Page "), true);
//footer.Alignment = Element.ALIGN_RIGHT;
//footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
//document1.Footer = footer;
//HeaderFooter header = new HeaderFooter(new Phrase(""), true);
//header.Alignment = Element.ALIGN_LEFT;
//header.Border = iTextSharp.text.Rectangle.NO_BORDER;
//document1.Add(header);
document1.Open();
PdfContentByte cb1 = writer1.DirectContent;
PdfImportedPage page1;
string test1 = TableofContent; int TableofContentPageCount = 0; int SectionPageStartNumber = 0;
string lastdocnamestr = "";
for (int t = 1; t <= pdfreader.NumberOfPages; t++)
{
document1.NewPage();
HeaderFooter header = new HeaderFooter(new Phrase(""), true);
header.Alignment = Element.ALIGN_LEFT;
header.Border = iTextSharp.text.Rectangle.NO_BORDER;
HeaderFooter header1 = new HeaderFooter(new Phrase(" "), true);
header1.Alignment = Element.ALIGN_LEFT;
header1.Border = iTextSharp.text.Rectangle.NO_BORDER;
HeaderFooter header2 = new HeaderFooter(new Phrase(" "), true);
header2.Alignment = Element.ALIGN_LEFT;
header2.Border = iTextSharp.text.Rectangle.NO_BORDER;
document1.Add(header);
document1.Add(header1);
document1.Add(header2);
page1 = writer1.GetImportedPage(pdfreader, t);
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
byte[] pdfcontent = pdfreader.GetPageContent(t);
//PdfDictionary dict = pdfreader.GetPageN(t);
string contentStream = System.Text.Encoding.Default.GetString(pdfcontent);
var contentByte = writer1.DirectContent;
contentByte.BeginText();
contentByte.SetFontAndSize(baseFont, 8);
var multiLineString = "";
var multiLineString1 = "";
string test = getBetween(contentStream, "#~#", "#~#");
if (test.Length > 0)
{
test1 = test1.Replace(test + ".", t.ToString());
DataTable dt = getdocdetailforheader(Convert.ToInt32(test.Replace("DID", "")), con);
if (dt.Rows.Count > 0)
{
multiLineString = dt.Rows[0]["DocumentName"].ToString() + " - " + Convert.ToDateTime(dt.Rows[0]["EncounterDTTM"].ToString()).ToString("MM/dd/yyyy") + " | Owner : " + dt.Rows[0]["Ownername"].ToString();
lastdocnamestr = multiLineString;
WriteLogEntry(multiLineString);
}
if (TableofContentPageCount == 0)
{
TableofContentPageCount = t;
}
}
//if (contentStream.Contains("sectionstart") && SectionPageStartNumber == 0) SectionPageStartNumber = t;
if (lastdocnamestr != string.Empty)
{
multiLineString = lastdocnamestr;
}
//else
//{
// if (TableofContentPageCount == 0)
// {
// multiLineString = "Table of Content";
// }
//}
multiLineString1 = patientheader;
contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, multiLineString, 15, 820, 0);
contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, multiLineString1, 15, 810, 0);
contentByte.EndText();
string relLogo = Directory.GetCurrentDirectory().ToString().Replace("bin", "").Replace("Debug", "").Replace("\\\\", "") + "\\Image\\MFA_LOGO.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(relLogo);
jpg.ScaleAbsolute(38f, 38f);
jpg.SetAbsolutePosition(document1.PageSize.Width - 70, 806);
jpg.Alignment = Element.ALIGN_RIGHT;
document1.Add(jpg);
cb1.MoveTo(0, 805);
cb1.LineTo(document1.PageSize.Width, 805);
cb1.Stroke();
cb1.AddTemplate(page1, 0, 0);
}
SectionPageStartNumber = pdfreader.NumberOfPages + 1;
System.Collections.ArrayList htmlarraylist1 = HTMLWorker.ParseToList(new StringReader(sectiondetails), null);
document1.NewPage();
for (int k = 0; k < htmlarraylist1.Count; k++)
{
document1.Add((IElement)htmlarraylist1[k]);
}
document1.Close();
FinalPDF(FinalOutputPath + filename + ".pdf", FinalOutputPath + filename + "_1.pdf", patientfirstpagestr, test1, TableofContentPageCount, patientheader, SectionPageStartNumber);
File.Delete(temppath + filename + "_Temp.pdf");

Water mark on image error inside list view asp dot net

i have this c# asp dot net code to add water mark on the asp image control which is working fine.
string watermarkText = "© water mark";
string fileName = Server.MapPath(myimg.ImageUrl);
FileStream fs = new FileStream(fileName, FileMode.Open);
using (Bitmap bmp = new Bitmap(fs, false))
{
using (Graphics grp = Graphics.FromImage(bmp))
{
Brush brush = new SolidBrush(Color.Red);
Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
SizeF textSize = new SizeF();
textSize = grp.MeasureString(watermarkText, font);
Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 80)));
grp.DrawString(watermarkText, font, brush, position);
using (MemoryStream memoryStream = new MemoryStream())
{
bmp.Save(memoryStream, ImageFormat.Png);
string base64String = Convert.ToBase64String(memoryStream.ToArray());
string imageUrl = "data:image/png;base64," + base64String;
myimg.Attributes.Add("src", imageUrl);
}
}
}
but when i add the same water mark code inside listview on listview databound event like
System.Web.UI.WebControls.Image myimg;
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (!IsPostBack)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
myimg = ((System.Web.UI.WebControls.Image)e.Item.FindControl("Image1"));
string watermarkText = "© watermark";
string fileName = Server.MapPath(myimg.ImageUrl);
FileStream fs = new FileStream(fileName, FileMode.Open);
using (Bitmap bmp = new Bitmap(fs, false))
{
using (Graphics grp = Graphics.FromImage(bmp))
{
Brush brush = new SolidBrush(Color.Red);
Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
SizeF textSize = new SizeF();
textSize = grp.MeasureString(watermarkText, font);
Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 80)));
grp.DrawString(watermarkText, font, brush, position);
using (MemoryStream memoryStream = new MemoryStream())
{
bmp.Save(memoryStream, ImageFormat.Png);
string base64String = Convert.ToBase64String(memoryStream.ToArray());
string imageUrl = "data:image/png;base64," + base64String;
myimg.Attributes.Add("src", imageUrl);
}
}
}
}
}
}
so it gives me following error CustomCoupon\ca00453f-c985-4794-9a87-36a60e2fa0e1.png' because it is being used by another process.
Please advice.
You can replace:
FileStream fs = new FileStream(fileName, FileMode.Open);
by:
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
...
}

The OnEndPage event and a table with a RowSpan

I added the creation of a table cone page header in OnEndPage. In table I a RowSpan that is not printed, while the rest is.
table.WriteSelectedRows (0, -1, pageSize.GetLeft (25), pageSize.GetTop (10), cb);
If I remove the RowSpan prints!
This is the code I use:
public override void OnEndPage(PdfWriter writer, Document document)
{
if (document.PageNumber != 1)
{
if (report.repeatHead) //ripete l'intestazione del report su tutte le pagine di stampa
{
repeatHead(writer, document);
}
else
{
if (document.PageNumber == 2) //ripete l'intestazione del report solo sulla second pagina dopo la copertina
{
repeatHead(writer, document);
}
}
}
}
public void repeatHead(PdfWriter writer, Document document)
{
//OnStartPage
base.OnStartPage(writer, document);
Rectangle pageSize = document.PageSize;
PdfPTable table = new PdfPTable(2);
//table.WidthPercentage = 100;
table.TotalWidth = pageSize.Width - 50;
table.DefaultCell.Border = Rectangle.NO_BORDER;
//impostazione larghezza celle
iTextSharp.text.Rectangle rect = PageSize.A4;
float pageWidth = rect.Width;
table.SetWidthPercentage(new float[]
{
(float).70 * pageWidth ,
(float).30 * pageWidth,
}, rect);
//Cella nome banca
table.AddCell(CellTest(report.banca, 1, 2));
//Cella descrizione indagine
table.AddCell(CellTest("Valore1", 0, 0));
//Cella data apertura
table.AddCell(CellTest("Data apertura: " + DateTime.Now, 0, 0));
//Cella descrizione
table.AddCell(CellTest("Descrizione", 0, 0));
//Cella data chiusura
table.AddCell(CellTest("Data chiusura: " + DateTime.Now, 0, 0));
table.WriteSelectedRows(0, -1, pageSize.GetLeft(25), pageSize.GetTop(10), cb);
}
private PdfPCell CellTest(string value, int colSpan, int rowSpan)
{
iTextSharp.text.Font font = FontFactory.GetFont("Arial");
PdfPCell c = new PdfPCell(new Phrase(value, font));
c.BorderWidthLeft = 1f;
c.BorderWidthTop = 1f;
c.BorderWidthRight = 1f;
c.BorderWidthBottom = 1f;
if (rowSpan != 0) { c.Rowspan = rowSpan; }
if (colSpan != 0) { c.Colspan = colSpan; }
return c;
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
template.BeginText();
template.SetFontAndSize(bf, 8);
template.SetTextMatrix(0, 0);
template.ShowText("" + (writer.PageNumber - 1));
template.EndText();
}

Cannot read the index page links after zooming in itextsharp

I have a PDF file which contains an Index Page that includes section with target page.
The "book15.pdf" default zooming percentage will be 68%, when I programmatically change the zooming percentage to 100% hyperlinks inside the original was lost? why?
here is My sample code.
Referred URL : http://wskidmore.com/2011/03/pdf-initial-view-settings-itextsharp/
string FileName = AppDomain.CurrentDomain.BaseDirectory + "BooK15.pdf";
Document doc = new Document();
PdfReader reader = new PdfReader(FileName);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
PdfAction zoom = PdfAction.GotoLocalPage(1, new PdfDestination(PdfDestination.XYZ, -1, -1, (float)Int32.Parse("100") / 100), writer);
writer.SetOpenAction(zoom);
doc.AddDocListener(writer);
PdfContentByte cb = writer.DirectContent;
for (int p = 1; p <= reader.NumberOfPages; p++)
{
doc.SetPageSize(reader.GetPageSize(p));
doc.NewPage();
PdfImportedPage page = writer.GetImportedPage(reader, p);
int rot = reader.GetPageRotation(p);
if (rot == 90 || rot == 270)
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0);
}
reader.Close();
doc.Close();
File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + "BooK151.pdf", memoryStream.ToArray());
}
When I read the index page page number links using below code:
int _count= pdfReader.GetLinks(PageNo).Count;
PdfDictionary PageDictionary = pdfReader.GetPageN(PageNo);
PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);
but this returns 0 only. Any ideas?
here is my screen shots...
Original PDF with click-able links
After converting the links were lost

datatype image in $.ajax

in page cap.aspx
==========================================================================
string code = GetRandomText();
Bitmap bitmap = new Bitmap(160,50,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bitmap);
Pen pen = new Pen(Color.LavenderBlush);
Rectangle rect = new Rectangle(0,0,160,50);
SolidBrush b = new SolidBrush(Color.LightPink);
SolidBrush blue = new SolidBrush(Color.White);
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(b, rect);
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(), new Font("Tahoma", 10 + rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
counter += 20;
}
DrawRandomLines(g);
bitmap.Save(Response.OutputStream,ImageFormat.Gif);
g.Dispose();
b.Dispose();
blue.Dispose();
bitmap.Dispose();
===================================================================================
<div id="DIVdialog" style="display:none">
<img src="cap.aspx"/>
</div>
===================================================================================
$("#DIVdialog").dialog();
==================================================================================
show dialog but does not show image? address cap.aspx is correct!
how get cap.aspx by $.ajax and datatype:image?
I think that the key here is to add the ContentType and the BufferOutput
context.Response.ContentType = "image/gif";
context.Response.BufferOutput = false;
Eg:
public void RenderIt(HttpContext context)
{
context.Response.ContentType = "image/gif";
context.Response.BufferOutput = false;
string code = GetRandomText();
using(Bitmap bitmap = new Bitmap(160,50,System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using(Graphics g = Graphics.FromImage(bitmap))
{
using(Pen pen = new Pen(Color.LavenderBlush)
{
using(SolidBrush b = new SolidBrush(Color.LightPink))
{
using(SolidBrush blue = new SolidBrush(Color.White))
{
Rectangle rect = new Rectangle(0,0,160,50);
int counter = 0;
g.DrawRectangle(pen, rect);
g.FillRectangle(b, rect);
for (int i = 0; i < code.Length; i++)
{
g.DrawString(code[i].ToString(), new Font("Tahoma", 10 + rand.Next(14, 18)), blue, new PointF(10 + counter, 10));
counter += 20;
}
DrawRandomLines(g);
g.Dispose();
b.Dispose();
blue.Dispose();
// Return
bitmap.Save(context.Response.OutputStream, ImageFormat.Gif);
// Dispose
bitmap.Dispose();
}
}
}
}
}
context.Response.End();
}

Resources