Friday, 15.02.2019, 22:03

Engineer M DevelopmentMA

Главная » 2013 » May » 1 » LOOK INSIDE & ABCpdf

How to do a look inside option for .NET framework?

The look inside option needs for looking a part of document. Through this user can partially learn the document and decide if data is equal with his requirements. 

The data of document keeps in HTML, image, pdf or bytes format. The data usually exports in the pdf file. For converting data in the pdf format exist more ways: ghostscript, library, on-line converter, utilities and etc. I chose the library ABCpdf created by WebSupergoo Software. It's flexible, understandable, accesible and with good, free tech support. This product has free 30-trial without any labels and watermarks. Price is small and adequate for a small company.

My problem is converting HTML to PDF and add a mask in all pages. The mask is a transparent image with blurred, dark, noised parts, which cover most of the main document data (text, images, equations). The mask size is 768x1024, equal a pdf page size.
mask look inside

In the project you have to add a reference at the ABCpdf library, which by default installed in the Program Files folder. For successfully coding have to define class:
using WebSupergoo.ABCpdf9; . 
The mask opens with class XImage:
XImage maskImg = new XImage();
The PDF document creates with class Doc and all actions performed with this class object:
Doc theDoc = new Doc();
After this you have to configure all necessary settings and options of document.
theDoc.HtmlOptions.UseJava = true; // enable Java data in html page
theDoc.HtmlOptions.UseScript = true;// enable Script data in html page
theDoc.HtmlOptions.InitialWidth = 1024;//default size of pdf page
theDoc.HtmlOptions.OnLoadScript = "(function(){ window.ABCpdf_go = false; setTimeout(function(){ window.ABCpdf_go = true; }, 3000); })();";//there you can write own scripts, this is script waiting 3 second before rendering 
theDoc.Rect.Inset(20, 20);//add border for all pages.

Next you have to add pages in the pdf document from the HTML data and cover with mask. The AddImageHtml() method doing rendering of HTML for one page. The AddImageObject() method covering data with mask. But if HTML has more data that can not fit in a page then you have to use a chainable function. Next code shows rendering of the chained HTML data from page to page:
theDoc.Page = theDoc.AddPage();//add first page
int theID = theDoc.AddImageHtml(htmlContent);//add all HTML data and get his ID
theDoc.AddImageObject(maskImg, true);//add the mask in first page
while (true){
    if (!theDoc.Chainable(theID))//check if exist chained page
        break;                           //exist from loop
    theDoc.Page = theDoc.AddPage();//add next chained page
    theID = theDoc.AddImageToChain(theID);//add data for next chained page
     theDoc.AddImageObject(maskImg, true);//add the mask for 
next chained page
maskImg.Clear();//clear memory of mask

Every page must be numerated:
for (int i = 1; i <= theDoc.PageCount; i++){
     theDoc.PageNumber = i;//set number
     theDoc.Flatten();//flattens and compresses the current page

Now you have the ready pdf document, but the data can be readed via a simple pdf  reader. Because the HTML data and the mask are separate objects in the document and you can copy necessary text (example). For resolving the issue you have to convert pages to images and save pdf document.
Doc theDocImgs = new Doc();//create document for images
for (int i = 1; i <= theDoc.PageCount; i++)//process every page
     theDocImgs.Page = theDocImgs.AddPage();
     theDoc.PageNumber = i;//set page, number starting at 1
     theDoc.Rendering.SaveQuality = 100;//quality of JPG image
     Byte[] data = theDoc.Rendering.GetData("JPG");//get data of image
     XImage image = new XImage();//create image object
     image.SetData(data);//set data
     theDocImgs.AddImageObject(image);//add image in current page
     image.Clear();//clear image memory
theDocImgs.Save("C://pdf_files/look_inside1.pdf");//save result page
theDocImgs.Clear();//clear document memory
theDoc.Clear();//clear document memory

The resulted pdf document will be copy protected (example). This library allow convert pdf to image in some formats and conversely. For it you can use XRendering object.
If a file belongs to certain user then you can add encryption. The file can be opened only with password:
if (!string.IsNullOrEmpty(password))
      theDocImgs.Encryption.Type = 2;
      theDocImgs.Encryption.CanCopy = false;
      theDocImgs.Encryption.OwnerPassword = password;
      theDocImgs.Encryption.Password = password;

This is ready and working code, you can change or combine some processes how you want. For more information please contact with WebSupergoo or me ;).
Категория: Lessons | Просмотров: 9199 | Добавил: admin | Рейтинг: 5.0/1 |
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]
Меню сайта
Разделы новостей
Concept Software [2]
Programs are planned or under construction.
Software [10]
Release Software
Lessons [12]
About Me [1]
Infa about yourself, documents, certificates ...
Documentation [4]
Documentation of projects
Календарь новостей
«  May 2013  »
Друзья сайта
Copyright Macarov Anatoli © 2019
Free website builderuCoz