将itext Watermark添加到Android Java PDF
我需要在我的PDF Android Java PDF文件中添加Itext Watermark 我用于创建PDF文件的示例当前代码:
File docsFolder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "osary/estmara");
String currentDate = new SimpleDateFormat("dd-MM-yyyy_hh_mm_aaa", Locale.getDefault()).format(new Date());
String pdfname = myEstmaraa.getTalabId() + "_" + currentDate + ".pdf";
pdfFile = new File(docsFolder, pdfname);
OutputStream output = new FileOutputStream(pdfFile);
Document document = new Document(PageSize.A4);
document.setMargins(5, 5, 5, 5);
PdfWriter PdfWriters = PdfWriter.getInstance(document, output);
PdfWriters.createXmpMetadata();
PdfWriters.setTagged();
document.open();
//todo 2 preparing Header and directions and margin row 0
PdfPTable tableForRowZero = new PdfPTable(new float[]{1});
tableForRowZero.setSpacingBefore(5);
tableForRowZero.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
tableForRowZero.getDefaultCell().setFixedHeight(34);
tableForRowZero.setTotalWidth(PageSize.A4.getWidth());
tableForRowZero.setWidthPercentage(100);
tableForRowZero.setHeaderRows(0);
tableForRowZero.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
cellss = new PdfPCell(new Phrase("رقم الاستمارة", FONT2));
cellss.setHorizontalAlignment(Element.ALIGN_CENTER);
cellss.setVerticalAlignment(Element.ALIGN_CENTER);
cellss.setBackgroundColor(BaseColor.GRAY);
tableForRowZero.addCell(cellss);
tableForRowZero.setHeaderRows(0);
cellss = new PdfPCell(new Phrase(String.valueOf(myEstmaraa.getTalabId()), FONT2));
cellss.setHorizontalAlignment(Element.ALIGN_CENTER);
cellss.setVerticalAlignment(Element.ALIGN_CENTER);
tableForRowZero.addCell(cellss);
document.add(tableForRowZero);
document.close();
,我遇到的问题就是这一行:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
当我将其更改为Android时,我会遇到一个问题,我尝试了:
com.itextpdf.kernel.pdf.PdfDocument pdfDoc = new com.itextpdf.kernel.pdf.PdfDocument(new PdfReader(pdfFile.getPath()),new PdfWriter(pdfFile.getPath()));
它给了我这个提示:
无法解析构造函数'pdfwriter(java.lang.string)'
我尝试制作水印的代码:
com.itextpdf.kernel.pdf.PdfDocument pdfDoc = new com.itextpdf.kernel.pdf.PdfDocument(new PdfReader(pdfFile.getPath()),new PdfWriter(pdfFile.getPath()));
PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(StandardFonts.HELVETICA));
com.itextpdf.layout.element.Paragraph paragraph2 = new com.itextpdf.layout.element.Paragraph("This watermark is added UNDER the existing content")
.setFont(font)
.setFontSize(15);
Canvas canvasWatermark1 = new Canvas(under, pdfDoc.getDefaultPageSize())
.showTextAligned(paragraph2, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
canvasWatermark1.close();
PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
over.setFillColor(ColorConstants.BLACK);
paragraph2 = new com.itextpdf.layout.element.Paragraph("This watermark is added ON TOP OF the existing content")
.setFont(font)
.setFontSize(15);
Canvas canvasWatermark2 = new Canvas(over, pdfDoc.getDefaultPageSize())
.showTextAligned(paragraph2, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
canvasWatermark2.close();
paragraph2 = new com.itextpdf.layout.element.Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
.setFont(font)
.setFontSize(15);
over.saveState();
// Creating a dictionary that maps resource names to graphics state parameter dictionaries
PdfExtGState gs1 = new PdfExtGState();
gs1.setFillOpacity(0.5f);
over.setExtGState(gs1);
Canvas canvasWatermark3 = new Canvas(over, pdfDoc.getDefaultPageSize())
.showTextAligned(paragraph2, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
canvasWatermark3.close();
over.restoreState();
我也尝试添加png映像并设置其不透明度,但它涵盖了内容
try {
com.itextpdf.text.Image image1_emp_osary = null;
Drawable d_emp_osary = getResources().getDrawable(R.drawable.ketm_estmara);
BitmapDrawable bitDw_emp_osary = ((BitmapDrawable) d_emp_osary);
Bitmap bmp_emp_osary = bitDw_emp_osary.getBitmap();
ByteArrayOutputStream stream_emp_osary = new ByteArrayOutputStream();
bmp_emp_osary.compress(Bitmap.CompressFormat.PNG, 100, stream_emp_osary);
image1_emp_osary = com.itextpdf.text.Image.getInstance(stream_emp_osary.toByteArray());
image1_emp_osary.scaleToFit(200, 200);
image1_emp_osary.setAlignment(Element.ALIGN_BOTTOM);
image1_emp_osary.setSpacingBefore(200);
image1_emp_osary.setTransparency(new int[]{5, 5});
document.add(new Chunk(image1_emp_osary, 0, 200));
PdfContentByte canvas = PdfWriters.getDirectContentUnder();
PdfGState state = new PdfGState();
state.setFillOpacity(0.6f);
canvas.setGState(state);
canvas.addImage(image1_emp_osary);
canvas.restoreState();
} catch (Exception e) {
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了,
这个想法是:
1.我们需要用排水标记保存文件,这很容易:
2。我们将打开文件,但我们不能直接覆盖它,
所以
:我们将其保存在带有水印的温度文件中,然后将其关闭:
b。我们将打开临时文件并使用旧名称保存:
i have solved it,
the idea is:
1.we need to save the file with out water mark ,and that was easy:
2.we will open the file but we can not override it directly,
so:
a. we will save it in a temp file with the watermark and close it:
b. we will open the temp file and save it with the old name: