Rank: Newbie
Groups: Registered
Joined: 5/17/2017(UTC) Posts: 1 Location: CDMX
|
Hi, I've made slightly changes to the code to test release 3.7.7.2704. The bitmap is added correctly in the screen, but it's not saved in the new pdf. What do I need to do? Is it a matter of a paid license? Can I use this SDK to build a UI canvas designer? Like a drawing tool? Is it possible to select programmatically a bitmap already added to the pdf? Even more, can I add a bitmap, save the document and after re-openning the pdf, select the added bitmap? (to rotate it perhaps, or changing its position?) Thanks in advance, vmchacon Code:
using Patagames.Pdf.Enums;
using Patagames.Pdf.Net;
using Patagames.Pdf;
using Patagames.Pdf.Net.BasicTypes;
private void pdfViewer1_MouseDoubleClick(object sender, MouseEventArgs e)
{
//Determine the page number where the mouse was clicked
int page = pdfViewer1.PointInPage(e.Location);
//( e.GetPosition(pdfViewer1));
//Translate the mouse coordinates to the page's coordinate system
var pointOnPage = pdfViewer1.ClientToPage(page, e.Location);
//GetPosition(pdfViewer1));
//Load bitmap from file and insert it into page
var bmp = System.Drawing.Bitmap.FromFile(@"C:\test.png") as System.Drawing.Bitmap;
var image = InsertImageToPage(pdfViewer1.Document, pdfViewer1.Document.Pages[page], bmp, pointOnPage);
pdfViewer1.ClearRenderBuffer();
pdfViewer1.Invalidate();
//InvalidateVisual();
//Insert image into page dictionary
InsertIntoDictionary(pdfViewer1.Document, pdfViewer1.Document.Pages[page], image);
//Save document
pdfViewer1.Document.Save(@"C:\test2.pdf", SaveFlags.NoIncremental);
}
public PdfImageObject InsertImageToPage(PdfDocument doc, PdfPage page, System.Drawing.Bitmap bmp, PointF atPoint)
{
var bi = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Create PdfBitmap object from .Net bitmap
var bitmap = new PdfBitmap(
bmp.Width,
bmp.Height,
BitmapFormats.FXDIB_Argb,
bi.Scan0,
bi.Stride);
//Create pdf image object and then set PdfBitmap object into it.
var image = PdfImageObject.Create(doc);
image.SetBitmap(bitmap);
//Scale image object to it's actual width and heihgt
image.SetMatrix(bmp.Width, 0, 0, bmp.Height, (float)atPoint.X, (float)atPoint.Y);
page.PageObjects.InsertObject(image);
return image;
}
public void InsertIntoDictionary(PdfDocument doc, PdfPage page, PdfImageObject image)
{
//Get page dictionary, list of indirect objects and original page content
var pageDict = page.Dictionary;
var list = PdfIndirectList.FromPdfDocument(doc);
//Convert contents to array.
PdfTypeArray array = ConvertContentsToArray(pageDict["Contents"], list, pageDict);
//Get stream of image.
IntPtr streamHandle = Pdfium.FPDFImageObj_GenerateStream(image.Handle, page.Handle);
var stream = PdfTypeStream.Create(streamHandle);
//Add image's stream into list of indirect objects and then add it to array.
int num = list.Add(stream);
array.AddIndirect(list, num);
}
public PdfTypeArray ConvertContentsToArray(PdfTypeBase contents, PdfIndirectList list, PdfTypeDictionary pageDict)
{
//check the original content whether it's an array
if (contents is PdfTypeArray)
return contents as PdfTypeArray; //if contents is a array just return it
else if (contents is PdfTypeIndirect)
{
if ((contents as PdfTypeIndirect).Direct is PdfTypeArray)
return (contents as PdfTypeIndirect).Direct as PdfTypeArray; //if contents is a reference to array then return that array
else if ((contents as PdfTypeIndirect).Direct is PdfTypeStream)
{
//if contents is a reference to a stream then create a new array and insert stream as a first element of array
var array = PdfTypeArray.Create();
array.AddIndirect(list, (contents as PdfTypeIndirect).Direct);
//Add array into list of indirect objects
list.Add(array);
//And set it as a contents of the page
pageDict.SetIndirectAt("Contents", list, array);
return array;
}
else
throw new Exception("Unexcpected content type");
}
else
throw new Exception("Unexcpected content type");
}
Edited by moderator Wednesday, May 24, 2017 9:43:32 AM(UTC)
| Reason: Not specified
|