Cannot read the index page links after zooming in itextsharp - asp.net

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

Related

Resize image on server without download

I have images present on server in asp.net. I want to create their thumbnails by just copying, resizing, renaming and at last saving them on server itself. For compression I have the code but how can I Save the file.
if (fileUploader.HasFile)
{
string fileName = Path.GetFileName(fileUploader.PostedFile.FileName);
string ext = string.Empty;
ext = System.IO.Path.GetExtension(fileUploader.FileName.ToString()).ToLower();
fileUploader.PostedFile.SaveAs(Server.MapPath("~/Images_Coach/" + hdnCoachId.Value + "/") + hdnCoachId.Value + ext);
int width = Convert.ToInt32(150);
int height = Convert.ToInt32(150);
Stream inp_Stream = fileUploader.PostedFile.InputStream;
using (var image = System.Drawing.Image.FromStream(inp_Stream))
{
Bitmap myImg = new Bitmap(width, height);
Graphics myImgGraph = Graphics.FromImage(myImg);
myImgGraph.CompositingQuality = CompositingQuality.HighQuality;
myImgGraph.SmoothingMode = SmoothingMode.HighQuality;
myImgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imgRectangle = new Rectangle(0, 0, width, height);
myImgGraph.DrawImage(image, imgRectangle);
newFile = hdnCoachId.Value + "_icon" + ext;
// Save the file
var path = Path.Combine(Server.MapPath("~/Images_Coach/" + hdnCoachId.Value + "/"), newFile);
myImg.Save(path, image.RawFormat);
}
}
I can get the file into byte array and then convert it into stream
foreach (var fileName in Directory.GetFiles(dirFile))
{
if (fileName.Contains(dir))
{
string newFile = string.Empty;
//Read the File into a Byte Array.
string ext = string.Empty;
ext = System.IO.Path.GetExtension(fileName.ToString()).ToLower();
byte[] bytes = File.ReadAllBytes(fileName);
Stream inp_Stream = new MemoryStream(bytes);
int width = Convert.ToInt32(150);
int height = Convert.ToInt32(150);
using (var image = System.Drawing.Image.FromStream(inp_Stream))
{
Bitmap myImg = new Bitmap(width, height);
Graphics myImgGraph = Graphics.FromImage(myImg);
myImgGraph.CompositingQuality = CompositingQuality.HighQuality;
myImgGraph.SmoothingMode = SmoothingMode.HighQuality;
myImgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imgRectangle = new Rectangle(0, 0, width, height);
myImgGraph.DrawImage(image, imgRectangle);
newFile = dir + "_icon" + ext;
// Save the file
var newPath = Path.Combine(Server.MapPath("~/Images_Coach/" + dir + "/"), newFile);
myImg.Save(newPath, image.RawFormat);
}
}
//File.Delete
// fileName is the file name
}

Asp.Net Jpeg Compression

I am saving original picture and its thumbnail but the thumbnail size is bigger than the original.
if (fu_photo.HasFile)
{
fu_photo.SaveAs(Server.MapPath("../media/" + fu_photo.FileName));
Bitmap orgimg = new Bitmap(fu_photo.FileContent);
double orgWidth = orgimg.Width;
double orgHeight = orgimg.Height;
double orgRatio = orgWidth / orgHeight;
int newWidth = 256;
int newHeight = Convert.ToInt32(Convert.ToDouble(newWidth) / orgRatio);
Bitmap newimg = new Bitmap(orgimg, newWidth, newHeight);
Graphics gimg = Graphics.FromImage(newimg);
gimg.InterpolationMode = InterpolationMode.HighQualityBicubic;
gimg.SmoothingMode = SmoothingMode.AntiAlias;
gimg.CompositingQuality = CompositingQuality.HighQuality;
gimg.PixelOffsetMode = PixelOffsetMode.HighQuality;
// gimg.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
gimg.DrawImage(newimg, 0, 0, newWidth, newHeight);
newimg.Save(Server.MapPath("../media/256_" + fu_photo.FileName));
orgimg.Dispose();
newimg.Dispose();
gimg.Dispose();
}
This is my code. I tried it with a 680x382px 93kB photo. It saved the original one at 93kB but it saved the thumbnail one at 256x144px and 97kB!
When I tried to save with Photoshop at high quality and 256x144px it saved it at 18kB.
How can I reduce the image file size?
because you map a low quality to high quality image,
and how about this?
FileUpload1.PostedFile.SaveAs(ServerPatch + fileName);
System.Drawing.Image thumb = image.GetThumbnailImage(_Width,_Height, () => false, IntPtr.Zero);
thumb.Save(ServerPatch + "/thumb/" + fileName);

OpenCL imaging - only one pixel is updated

I'm using Opencl.net and I'm trying to do some image processing on the GPU. Unfortunately only the first pixel ([0;0]) has the correct value, and the rest is (0;0;0;0). The OpenCL kernel should assign 0.5 to all color components of every pixel. It seems to me that the kernel is being executed only once (or perhaps the read function is reading only the first pixel). What am I doing wrong? I've omitted not relevant parts from my code:
...
int intPtrSize = 0;
intPtrSize = Marshal.SizeOf(typeof(IntPtr));
Cl.Mem srcImage2DBuffer;
Cl.ImageFormat imageFormat = new Cl.ImageFormat(Cl.ChannelOrder.ARGB, Cl.ChannelType.Float);
int imgWidth = 0, imgHeight = 0;
IntPtr srcFloatDataPtr;
int srcIMGBytesSize = 0;
GCHandle pinnedSrcFloatArray;
//Load image from file into OpenCL buffer
using (FileStream imageFileStream = new FileStream(inputImagePath, FileMode.Open) ) {
System.Drawing.Image inputImage = System.Drawing.Image.FromStream( imageFileStream );
imgWidth = inputImage.Width;
imgHeight = inputImage.Height;
System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(inputImage);
BitmapData bitmapData = bmpImage.LockBits( new Rectangle(0, 0, bmpImage.Width, bmpImage.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
srcIMGBytesSize = bitmapData.Stride * bitmapData.Height;
//Convert image from byte to float array
byte[] inputByteArray = new byte[srcIMGBytesSize];
Marshal.Copy(bitmapData.Scan0, inputByteArray, 0, srcIMGBytesSize);
bmpImage.UnlockBits( bitmapData );
float[] inputFloatArray = new float[srcIMGBytesSize];
Array.Copy(inputByteArray, inputFloatArray, srcIMGBytesSize);
for (int i = 0; i < srcIMGBytesSize; i++) {
inputFloatArray[i] /= 255.0f;
}
pinnedSrcFloatArray = GCHandle.Alloc(inputFloatArray, GCHandleType.Pinned);
srcFloatDataPtr = pinnedSrcFloatArray.AddrOfPinnedObject();
srcImage2DBuffer = Cl.CreateImage2D(_context, Cl.MemFlags.CopyHostPtr | Cl.MemFlags.ReadOnly, imageFormat,
(IntPtr)bitmapData.Width, (IntPtr)bitmapData.Height,
(IntPtr)0, srcFloatDataPtr, out error);
}
float[] outputFloatArray = new float[srcIMGBytesSize];
//I'm not sure whether the pointer here is correct or not.
Cl.Mem resultImage2DBuffer = Cl.CreateImage2D(_context, Cl.MemFlags.CopyHostPtr | Cl.MemFlags.WriteOnly, imageFormat,
(IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)0, outputFloatDataPtr, out error);
error = Cl.SetKernelArg(kernel, 0, (IntPtr)intPtrSize, srcImage2DBuffer);
error |= Cl.SetKernelArg(kernel, 1, (IntPtr)intPtrSize, resultImage2DBuffer);
...
IntPtr[] originPtr = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
IntPtr[] regionPtr = new IntPtr[] { (IntPtr)1, (IntPtr)1, (IntPtr)1 };
IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 };
error = Cl.EnqueueWriteImage(cmdQueue, srcImage2DBuffer, Cl.Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, srcFloatDataPtr, 0, null, out clevent);
pinnedSrcFloatArray.Free();
error = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 2, null, workGroupSizePtr, null, 0, null, out clevent);
error = Cl.EnqueueReadImage(cmdQueue, resultImage2DBuffer, Cl.Bool.True, originPtr, regionPtr,
(IntPtr)0, (IntPtr)0, outputFloatArray, 0, null, out clevent);
for (int i = 0; i < srcIMGBytesSize; i++) {
outputFloatArray[i] *= 255.0f;
}
//Right here I'm learning that all of the components are 0
for (int i = 0; i < srcIMGBytesSize; i+=4) {
Console.WriteLine("(" + outputFloatArray[i] + "; " + outputFloatArray[i+1] + "; "
+ outputFloatArray[i+2] + "; " + outputFloatArray[i+3] + ")");
}
Thank you!
I've figured out the problem. The region in Cl.EnqueueWriteImage/Cl.EnqueueReadImage should be (imageWidth, imageHeight, 1) instead of (1, 1, 1):
IntPtr[] regionPtr = new IntPtr[] { (IntPtr)imgWidth, (IntPtr)imgHeight, (IntPtr)1 };

Itextsharp: Is There Any Way To Copy Links When Import Pages?

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);
}

Header, footer and large tables with iTextSharp

I've added an header and a footer on my document by PdfPageEventHelper.
The document has several "large" tables populated at runtime.
I've tried to add those tables simply by "documen.Add(table)", but my header and my footer results overwritten.
I've already tried both methods to add the tables (WriteSelectedRows and document.Add(myPdfPtable).
Here is the code of the PageEventHelper:
private class MyPageEventHandler : PdfPageEventHelper
{
public iTextSharp.text.Image ImageHeader { get; set; }
public iTextSharp.text.Image ImageFooter { get; set; }
public override void OnEndPage(PdfWriter writer, Document document)
{
var fontintestazione = FontFactory.GetFont("Verdana", 10, Font.BOLD, BaseColor.LIGHT_GRAY);
var fontRight = FontFactory.GetFont("Verdana", 8, Font.BOLD, BaseColor.WHITE);
var fontFooter = FontFactory.GetFont("Verdana", 6, Font.NORMAL, BaseColor.BLUE);
float cellHeight = document.TopMargin;
Rectangle page = document.PageSize;
PdfPTable head = new PdfPTable(3);
head.TotalWidth = page.Width;
PdfPCell c = new PdfPCell(ImageHeader, true);
c.HorizontalAlignment = Element.ALIGN_LEFT;
c.FixedHeight = cellHeight;
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase("somePhrase", fontintestazione));
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase("someTextBlah", fontRight));
c.Border = PdfPCell.NO_BORDER;
c.HorizontalAlignment = 1;
c.BackgroundColor = new BaseColor(70, 130, 180);
head.AddCell(c);
head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight -30, writer.DirectContent);
PdfPTable footer = new PdfPTable(2);
footer.TotalWidth = 316f;
float[] cfWidths = new float[] { 2f, 1f };
footer.SetWidths(cfWidths);
PdfPCell cf = new PdfPCell(ImageFooter, true);
cf.HorizontalAlignment = Element.ALIGN_RIGHT;
cf.FixedHeight = cellHeight;
cf.Border = PdfPCell.NO_BORDER;
footer.AddCell(cf);
cf = new PdfPCell(new Phrase("someEndingText", fontFooter));
cf.HorizontalAlignment = Element.ALIGN_LEFT;
cf.Border = PdfPCell.NO_BORDER;
footer.AddCell(cf);
footer.WriteSelectedRows(0, -1, 10, 50, writer.DirectContent);
}
On my page, i simply do:
var document = new Document(PageSize.A4);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/header.ong"));
iTextSharp.text.Image imageFooter = iTextSharp.text.Image.GetInstance(Server.MapPath("/images/footer.png"));
MyPageEventHandler eve = new MyPageEventHandler
{
ImageHeader = imageHeader ,
ImageFooter = imageFooter
};
writer.PageEvent = eve;
document.Open();
//adding a table
PdfPTable cvTable = new PdfPtable(3);
cvTable.TotalWidth = document.PageSize.Width;
PdfPCell hCell = new PdfPCell(new Phrase("Jobs By User", aCustomFont));
cvTable.AddCell(hCell);
for(int i = 0; i < myTable.Records.Count; i++)
{
PdfPCell idCell = new PdfPCell(new Phrase(myTable.Records[i]._id, aFont));
cvTable.Add(idCell);
//same stuff for other fields of table
}
//first attempt.... failed:
document.Add(cvTable) //<- header and footer are overwritten by table
//second attempt..... failed too...
cvTable.WriteSelectedRows(0, -1, 10, myPoisition, writer.DirectContent);
//kind of fail...:
//the table is large and need more pages. It is trunked on the first page and overwrite
//the footer.
In your OnEndPage method you have this line:
head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight - 30, writer.DirectContent);
That code correctly calculates where to put content based on the page's height and top margin but also includes a magical 30 in there which is causing the header to be drawn on top of the table. Change it to this and your header will be fine.
head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight, writer.DirectContent);
I'm guessing that that 30 is trying to include some padding between your header and the table itself. What I would recommend is actually changing the document's margins themselves in the main code:
document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin);
And then accounting for that in the OnEndPage method:
float cellHeight = document.TopMargin - 30;
Your footer code doesn't actually account for the bottom margin and just draws it at 50 so this will always overlap. The quick fix would be to change it to:
footer.WriteSelectedRows(0, -1, 10, footer.TotalHeight, writer.DirectContent);
This will at least get the footer bottom-aligned. If you want some more padding like above just adjust the document margins again:
document.SetMargins(document.LeftMargin, document.RightMargin, document.TopMargin + 30, document.BottomMargin + 30);

Resources