Home > Back-end >  For help each elder questions about using pdfbox add pictures! Urgent please!
For help each elder questions about using pdfbox add pictures! Urgent please!

Time:04-02

Would like to ask why my pictures cannot be displayed, but you can click on the image display area, click can pop up after the picture information code is as follows:
 private InputStream createVisualSignatureTemplate3 (PDDocument srcDoc, PDRectangle the rect, BufferedImage image, int pageNum, 
PDSignature signature) throws IOException {

PDDocument doc=new PDDocument ();
PDPage page=new PDPage (srcDoc getpages (pageNum). GetMediaBox ());
Doc. The addPage (page);

PDAcroForm acroForm=new PDAcroForm (doc);
Doc. GetDocumentCatalog (.) setAcroForm (acroForm);

PDSignatureField signatureField=new PDSignatureField (acroForm);
PDAnnotationWidget widget=signatureField. GetWidgets (). The get (0);
SignatureField. SetValue (signature);

List AcroFormFields=acroForm. The getFields ();
AcroForm. SetSignaturesExist (true);
AcroForm. SetAppendOnly (true);
AcroForm. GetCOSObject (.) setDirect (true);
AcroFormFields. Add (signatureField);

Widget. SetRectangle (the rect);
Widget. SetPage (page);
Page. GetAnnotations (). The add (widget);

//create the first Form (N)
PDStream stream=new PDStream (doc);
PDFormXObject form=new PDFormXObject (stream);
PDResources res=new PDResources ();
Form. SetResources (res);
Form. SetFormType (1);
PDRectangle bbox=new PDRectangle (the rect getWidth (), the rect. GetHeight ());
Form. SetBBox (bbox);

//create ProcSet
COSArray procSet=new COSArray ();
ProcSet. Add (COSName. GetPDFName (" PDF "));
ProcSet. Add (COSName. GetPDFName (" Text "));
ProcSet. Add (COSName. GetPDFName (" ImageB "));
ProcSet. Add (COSName. GetPDFName (" ImageC "));
ProcSet. Add (COSName. GetPDFName (" ImageI "));
Res. GetCOSObject (.) setItem (COSName PROC_SET, procSet);

//create FRM
PDStream stream1=new PDStream (doc);
PDFormXObject FRM=new PDFormXObject (stream1);
Res. The put (COSName getPDFName (FRM), FRM);

//add FRM attribute
FRM. SetFormType (1);
FRM. SetBBox (bbox);
PDResources res1=new PDResources ();
FRM. SetResources (res1);

//create ExGState and add attribute
PDExtendedGraphicsState extGState=new PDExtendedGraphicsState ();
ExtGState. SetAlphaSourceFlag (false);
ExtGState. SetBlendMode (BlendMode. DARKEN);
ExtGState. SetNonStrokingAlphaConstant (1.0 f);
ExtGState. SetStrokingOverprintControl (true);
Res1. Put (COSName. GetPDFName (" NtkoExt "), extGState);

//add again ProcSet
Res1. GetCOSObject (.) setItem (COSName PROC_SET, procSet);


ByteArrayOutputStream bao=new ByteArrayOutputStream ();
ImageIO. Write (image, "PNG", bao);
Bao. Flush ();
Byte [] imageByte=bao. ToByteArray ();
Bao. Close ();

BufferedImage clone=new BufferedImage (image. GetWidth (), image, getHeight (), BufferedImage. TYPE_4BYTE_ABGR);
Graphics2D g2d=clone. CreateGraphics ();
G2d. SetRenderingHint (RenderingHints KEY_ANTIALIASING, RenderingHints VALUE_ANTIALIAS_ON);

G2d. DrawImage (image, 0, 0, null);
G2d. The dispose ();

Int width2=clone. GetWidth ();
Int height2=clone. GetHeight ();

for (int i=0; I & lt; Width2; I++) {
For (int j=0; J & lt; Height2; J++) {
Int RGB=clone. GetRGB (I, j);
Int b=(0 XFF & amp; RGB);
Int g=(0 XFF & amp; (RGB & gt;> 8));
Int r=(0 XFF & amp; (RGB & gt;> 16));
Int gray=(int) (r g + + 0.59 * 0.11 * 0.3 * b);

If ((r & gt; 120) & amp; & (g & lt; 250) & amp; & (b & lt; 250)) {

RGB=gray + (gray & lt; & lt; 8) + (gray & lt; & lt; 16) + (180 & lt; & lt; 24);

} else {

RGB=gray + (gray & lt; & lt; 8) + (gray & lt; & lt; 16) + (1 & lt; & lt; 24);

}

Clone. SetRGB (I, j, RGB);
}
}

ByteArrayOutputStream bao2=new ByteArrayOutputStream ();
ImageIO. Write (clone, PNG, bao2);
Bao2. Flush ();
Byte [] imageByte2=bao2. ToByteArray ();
Bao2. Close ();



//create image
PDImageXObject ximage=PDImageXObject. CreateFromByteArray (doc, imageByte, null);
PDImageXObject ximage2=PDImageXObject. CreateFromByteArray (doc, imageByte2, null);

COSDictionary images=new COSDictionary ();
Images. SetItem (" Sign0 ximage);
Images. SetItem (" Sign1 ximage2);


Res1. GetCOSObject (.) setItem (COSName XOBJECT, images);


PDAppearanceDictionary appearance=new PDAppearanceDictionary ();
Appearance. GetCOSObject (.) setDirect (true);
PDAppearanceStream appearanceStream=new PDAppearanceStream (form getCOSObject ());
Appearance. SetNormalAppearance (appearanceStream);
Widget. SetAppearance (appearance);


//unable to display picture

PDAppearanceStream PDAppearanceStream=new PDAppearanceStream (FRM) getCOSObject ());
PDPageContentStream contentStream=new PDPageContentStream (doc, pdAppearanceStream);

If (isDecryption) {

ContentStream. DrawImage (ximage, 0, 0, the rect getWidth (), the rect. GetHeight ());

} else {

ContentStream. DrawImage (ximage2, 0, 0, the rect getWidth (), the rect. GetHeight ());
}
nullnullnullnullnullnullnullnull
  • Related