DOMINO中实现PDF在线编辑控件 and so on......(三)

 三、iText
    OCX开发完了,就是在服务器生成和处理DOMINO中的PDF文档了。这里使用的是著名的开源jar包:iText。一个生成PDF的HelloWorld:
       
package  com.lowagie.examples.general.webapp;

import  java.io.IOException;
import  java.util.Date;

import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  com.lowagie.text.Document;
import  com.lowagie.text.DocumentException;
import  com.lowagie.text.Paragraph;
import  com.lowagie.text.html.HtmlWriter;
import  com.lowagie.text.pdf.PdfWriter;
import  com.lowagie.text.rtf.RtfWriter2;

/** */ /**
 * Hello World example as a Servlet.
 * 
 * 
@author blowagie
 
*/

public   class  HelloWorldServlet  extends  HttpServlet  {
       
    
private static final long serialVersionUID = -6033026500372479591L;

    
/** *//**
     * Returns a PDF, RTF or HTML document.
     * 
     * 
@see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     
*/

    
public void doGet (HttpServletRequest request, HttpServletResponse response)
    
throws IOException, ServletException {
        
        
// we retrieve the presentationtype
        String presentationtype = request.getParameter("presentationtype");
        
        
// step 1
        Document document = new Document();
        
try {
            
// step 2: we set the ContentType and create an instance of the corresponding Writer
            if ("pdf".equals(presentationtype)) {
                response.setContentType(
"application/pdf");
                PdfWriter.getInstance(document, response.getOutputStream());
            }

            
else if ("html".equals(presentationtype)) {
                response.setContentType(
"text/html");
                HtmlWriter.getInstance(document, response.getOutputStream());
            }

            
else if ("rtf".equals(presentationtype)) {
                response.setContentType(
"text/rtf");
                RtfWriter2.getInstance(document, response.getOutputStream());
            }

            
else {
                response.sendRedirect(
"http://itextdocs.lowagie.com/tutorial/general/webapp/index.html#HelloWorld");
            }

            
            
// step 3
            document.open();
            
            
// step 4
            document.add(new Paragraph("Hello World"));
            document.add(
new Paragraph(new Date().toString()));
        }

        
catch(DocumentException de) {
            de.printStackTrace();
            System.err.println(
"document: " + de.getMessage());
        }

        
        
// step 5: we close the document (the outputstream is also closed internally)
        document.close();
    }

}


    四、Servlet及拷屏限制等等
     Servlet及相关JAR包的部署见Domino中部署Servlet
     Servlet中取Domino附件参考Java 对 Domino Objects 的访问
     PDF加密:PDF可以通过对本身文件的加密实现权限限制,如控制打印,windowUI,menu等隐藏等功能。
     拷屏限制:这是我认为的一个很白痴的功能,但很多的甲方项目经理都会要这个象征意义大于实际意义的东西,真不知道他们关于这种信息安全的概念从何而来。可阅读的文档,要留存其内容,可以手工抄写,软件拷屏,拍照等等,方式太多了。方法:
禁止屏幕拷贝的办法倒是有一个,但是我得告诉你,要阻止其它应用程序从你的窗口上复制像素内容是不可能的。许多第三方程序都能捕获屏幕内容,这种程序也不难写。要想截获屏幕上的像素,你只要用 BitBlt 从屏幕设备上下文中拷贝它们既可,例如:

 

 

CWindowDC dc(NULL);   // 用 NULL 获取整个屏幕
CDC memdc;
... // 创建, 初始化 memdc
memdc.BitBlt(..., &dc); // 拷贝屏幕内容

  若要复制当前活动窗口的内容,只要获取该窗口的 CWnd 指针,然后用它来构造一个 CWindowDC,即可从中提取内容。
  总之,你无法阻止其它程序截获你窗口的像素。那么,如果你仅仅只是要禁用“屏幕拷贝”,或是阻止该功能做些什么,那其实很容易。Windows 通过注册热键来实现“屏幕 拷贝”功能。在我 2000 年 12 月的栏目中,我示范了如何用 RegisterHotKey 来注册应用程序热键(参见 C++ Q&A: Sending Messages in Windows, Adding Hot Keys to your Application),Windows 使用预定义的热键 IDHOT_SNAPDESKTOP 和 IDHOT_SNAPWINDOW 来处理“屏幕 拷贝”。这两个热键分别对应于“Print Screen”和“Alt+Print Screen”,前者用来复制整个屏幕,而后者则仅复制当前活动窗口。 为了禁用这些功能,你只要注册这些热键,当用户按下这些热键时,让 Windows 向你的程序发送 WM_HOTKEY 消息,此时你可以忽略这些消息, 旁路掉默认的屏幕复制行为既可。你的主框架(mainframe)类是最适合做这件事的地方。

// 热键的处理方法

// MainFrame.h
#include "FolderFrame.h"
#include "resource.h"

// Typical MFC Main frame window, override to disable PrintScreen.
//
class CMainFrame : public CFrameWnd {
protected:
...
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
// disable PrintScreen
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
afx_msg LRESULT OnHotKey(WPARAM wp, LPARAM lp);
afx_msg void OnDestroy();
DECLARE_MESSAGE_MAP()
};
MainFrame.cpp
#include "StdAfx.h"
#include "MainFrm.h"
#include "View.h"
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
...
// disable PrintScreen:
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_ACTIVATE()
ON_MESSAGE(WM_HOTKEY, OnHotKey)
END_MESSAGE_MAP()
...
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
...
RegisterHotKey(m_hWnd, IDHOT_SNAPDESKTOP, 0, VK_SNAPSHOT);
return 0;
}
void CMainFrame::OnDestroy()
{
UnregisterHotKey(m_hWnd, IDHOT_SNAPDESKTOP);
}
//
// Handle hotkey: should be PrintScreen or Alt-PrintScreen.
// Do nothing (bypass Windows screen capture)
//
LRESULT CMainFrame::OnHotKey(WPARAM wp, LPARAM)
{
UNREFERENCED_PARAMETER(wp);
return 0; // ignore
}
//
// When window is activated/deactivated, disable/enable Alt-PrintScreen.
// (IDHOT_SNAPWINDOW)
//
void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther,
BOOL bMinimized)
{
CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
if (nState)
RegisterHotKey(m_hWnd, IDHOT_SNAPWINDOW, MOD_ALT, VK_SNAPSHOT);
else
UnregisterHotKey(m_hWnd, IDHOT_SNAPWINDOW);
}      

  上述代码段展示了一个典型的 MFC CMainFrame 类实现。OnCreate/OnDestroy 函数用来注册/注销 IDHOT_SNAPDESKTOP 热键;OnActivate 函数用来在应用程序 处于激活/和非激活状态时注册/注销 IDHOT_SNAPWINDOW 热键。当你的窗口处于非激活状态时,通过重新启用 IDHOT_SNAPWINDOW,当别的应用程序拥有焦点时,用户仍然能用 Alt+Print Screen 来复制屏幕。
  你也许会想到用 CS_OWNDC 式样来注册窗口类以防止屏幕拷贝(它导致 Windows 为窗口类分配一个私有设备上下文),但那样做行不通。Windows 还是会把私有 DC 中的 像素位复制到屏幕 DC 中,这样一来,任何存取屏幕 DC 的程序都能看到你的像素。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值