java中使用pdfbox对pdf文件进行 *** 作时,如何实现插入文本的自动换行 *** 作?

java中使用pdfbox对pdf文件进行 *** 作时,如何实现插入文本的自动换行 *** 作?,第1张

刚才我这也在找这个答案……不过现在我写出来了,我的这段代码(通过控制纵坐标来换行):

int rows = 700

String str = text.getText()

String[] outs = str.split("\n")

PDFdoc = new PDDocument()

PDPage page = new PDPage()

PDFdoc.addPage(page)

PDFont font = PDType1Font.TIMES_ROMAN

PDPageContentStream contentStream = new PDPageContentStream(PDFdoc, page)

for(int i=0i<outs.lengthi++)

{

if(rows-FontSize>100)

rows -= FontSize

else

{

contentStream.close()

page = new PDPage()

PDFdoc.addPage(page)

contentStream = new PDPageContentStream(PDFdoc, page)

rows = 700

}

contentStream.beginText()

contentStream.moveTextPositionByAmount( 100, rows )

contentStream.setFont( font, FontSize )

contentStream.drawString( outs[i] )

contentStream.endText()

}

contentStream.close()

PDFdoc.save(new FileOutputStream(fdoc))

PDFdoc.close()

needSave = false

setTitle(Program.ProgramName+" - "+fdoc.getName())

注:FontSize是字体大小,我暂时设定行距为0,你可以自己再改改,换行大概就是这个思路了

pdf一页的纵坐标应该是从上到下800到0,保留一定的边距的话从700到100就是整个打印文本的范围。应该是这样的,我只是为了赶我们变态的 *** 作系统实验作业,没有再多去尝试(吐槽:这明显跟 *** 作系统没有任何关系,我们老师还拿这个当作业,难为我们也不是这样难为的…… *** 作系统有难题不布置,偏偏布置这种恶心人的题,最后贴上

普通 *** 作系统实验考试:内存管理,从页表地址到实际物理地址的转换;文艺 *** 作系统实验考试:nachos的结构分析(暂定,欢迎更改);二逼 *** 作系统实验考试:文件 *** 作,pdf文件的读写

可以用Spire.Pdf for Java类库给PDF文档添加附件,下面的代码是插入Excel和Word附件给你参考:

import com.spire.pdf.annotations.*

import com.spire.pdf.attachments.PdfAttachment

import com.spire.pdf.graphics.*

import java.awt.*

import java.awt.geom.Dimension2D

import java.awt.geom.Rectangle2D

import java.io.File

import java.io.FileInputStream

import java.io.IOException

public class AttachFiles {

public static void main(String[] args) throws IOException {

//创建PdfDocument对象

PdfDocument doc = new PdfDocument()

//加载PDF文档

doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf")

//添加附件到PDF

PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\使用说明书.docx")

doc.getAttachments().add(attachment)

//绘制标签

String label = "财务报表.xlsx"

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true)

double x = 35

double y = doc.getPages().get(0).getActualSize().getHeight() - 200

doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y)

//添加注释附件到PDF

String filePath = "C:\\Users\\Administrator\\Desktop\\财务报表.xlsx"

byte[] data = toByteArray(filePath)

Dimension2D size = font.measureString(label)

Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15)

PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data)

annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)))

annotation.setFlags(PdfAnnotationFlags.Default)

annotation.setIcon(PdfAttachmentIcon.Graph)

annotation.setText("点击打开财务报表.xlsx")

doc.getPages().get(0).getAnnotationsWidget().add(annotation)

//保存文档

doc.saveToFile("Attachments.pdf")

}

//读取文件到byte数组

public static byte[] toByteArray(String filePath) throws IOException {

File file = new File(filePath)

long fileSize = file.length()

if (fileSize >Integer.MAX_VALUE) {

System.out.println("file too big...")

return null

}

FileInputStream fi = new FileInputStream(file)

byte[] buffer = new byte[(int) fileSize]

int offset = 0

int numRead = 0

while (offset <buffer.length &&(numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {

offset += numRead

}

if (offset != buffer.length) {

throw new IOException("Could not completely read file "

+ file.getName())

}

fi.close()

return buffer

}

}

效果:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11926162.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存