我需要在内存中创建后将iTextPDF Document文件转换为byte [].我已经测试过,我没有正确创建PDF的问题.问题是如何将其转换为字节数组以存储在DB中.
这是我的代码:
Document generatedDocument = reportService.generateRequestForm(scdUser, jsonObject, 0, null);
reportService.generateRequestForm(scdUser, jsonObject, 0, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(generatedDocument, baos);
generatedDocument.open();
document.setDocument(baos.toByteArray()); // stores as blob
Run Code Online (Sandbox Code Playgroud)
我在数据库blob列中获得null值.
这是我的Document域对象:
文档域对象
@Entity
@Table(name = "document")
public class Document implements java.io.Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "document_id", nullable = false)
private int documentId;
@Column(name = "document_name", nullable = false, length = 65535)
private String documentName;
@Column(name …Run Code Online (Sandbox Code Playgroud) 我正在使用java在itext 5上工作.我的页面包含多个动态行表.在某些情况下,表格的最后一行将使用下面的标题拆分为下一页.我正在使用setHeaderRows()并setSkipFirstHeader()管理下一页的继续.最后一行有足够的空间放在前面的页面上.我想将最后一行放在同一页面而不是下一页.
例如,在第1页上,最后一行被拆分为下一页的第一行.相反,我想在第1页中安装该行,因此请保留一个包含所有空白的额外页面.
我尝试过使用setExtendLastRow(),但它不起作用.有谁知道如何解决这个问题.我附上了一份工作示例代码.
public class ProposalItextSplitLastRow {
public static void main(String[] args) {
try {
Document document = new Document();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 14, 14);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/SplitLastRow.pdf"));
document.open();
document.setPageSize(PageSize.LETTER);
document.setMargins(16, 14, 42, 38);
for (int m = 1; m < 20; m++) {
int row = 0;
PdfPTable table = new PdfPTable(1);
table.setSpacingAfter(0);
table.setSpacingBefore(0);
table.setWidthPercentage(100);
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
add(table, "Header Row continued " + m, BaseColor.LIGHT_GRAY, row++);
add(table, "Header Row …Run Code Online (Sandbox Code Playgroud) 我想知道JasperReports和iText/iTextpdf的优缺点.直到现在,我一直在研究JasperReports,但现在正考虑转向iText.我应该更改为iText还是继续使用JasperReports?
我知道对于动态报告,我们应该使用iText,对于具有静态结构的报告,我们可以使用JasperReports然后从它们创建pdf.但我仍然想了解更多有关这两方面的信息.
我想更多地了解Rectangle的坐标,特别是:
每次,我都会对如何根据这些坐标绘制尺寸来绘制矩形感到困惑.
如果可能的话,我可以简要地获得关于这些坐标位置的图形表示吗?
我想在iTextPDf中显示Special Character India Rupee Symbol,
我的代码:
Font fontRupee = FontFactory.GetFont("Arial", "?", true, 12);
Chunk chunkRupee = new Chunk(" ? 5410", font3);
Run Code Online (Sandbox Code Playgroud) 我有一个String由恒定部分和可变部分组成的。我希望变量在文本段落中使用常规字体设置格式,而我希望常量部分为粗体。
这是我的代码:
String cc_cust_name = request.getParameter("CC_CUST_NAME");
document.add(new Paragraph(" NAME " + cc_cust_name, fontsmallbold));
Run Code Online (Sandbox Code Playgroud)
我在表中单元格的代码如下所示:
cell1 = new PdfPCell(new Phrase("Date of Birth" + cc_cust_dob ,fontsmallbold));
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,第一部分(" NAME "和"Date of Birth")应为粗体,可变部分(cc_cust_name和cc_cust_dob)应为规则。
这个问题几乎说明了一切.我得到以下异常:
ExceptionConverter: java.io.IOException: /fonts/CALIBRI.TTF not found as file or resource.
Caused By: java.io.IOException: /fonts/CALIBRI.TTF not found as file or resource.
at com.itextpdf.text.io.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:263)
at com.itextpdf.text.io.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:173)
at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:148)
at com.itextpdf.text.pdf.TrueTypeFont.process(TrueTypeFont.java:641)
at com.itextpdf.text.pdf.TrueTypeFont.<init>(TrueTypeFont.java:375)
at com.itextpdf.text.pdf.BaseFont.getAllFontNames(BaseFont.java:1229)
at com.itextpdf.text.FontFactoryImp.register(FontFactoryImp.java:446)
at com.itextpdf.text.FontFactory.register(FontFactory.java:341)
at bean.createPdf.makePdf(createPdf.java:358)
at bean.auswahl.buttonProbenbegleitschein(auswahl.java:188)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at org.apache.myfaces.trinidad.component.UIXComponentBase.broadcast(UIXComponentBase.java:1113)
at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:179)
at org.apache.myfaces.trinidad.component.UIXComponent.broadcastInContext(UIXComponent.java:364)
at oracle.adf.view.rich.event.ProxyEvent.broadcastWrappedEvent(ProxyEvent.java:72)
at oracle.adf.view.rich.component.fragment.UIXRegion.broadcast(UIXRegion.java:124)
at org.apache.myfaces.trinidad.component.UIXComponent.broadcastInContext(UIXComponent.java:364)
at org.apache.myfaces.trinidad.component.WrapperEvent.broadcastWrappedEvent(WrapperEvent.java:82)
at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent$1.run(ContextSwitchingComponent.java:168)
at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent._processPhase(ContextSwitchingComponent.java:510)
at oracle.adf.view.rich.component.fragment.ContextSwitchingComponent.broadcast(ContextSwitchingComponent.java:171)
at …Run Code Online (Sandbox Code Playgroud) 我正在使用iText7(java),我正在寻找一种方法将PDF页面转换为图像.在较旧的iText版本中,您可以这样做:
PdfImportedPage page = writer.getImportedPage(reader, 1);
Image image = Image.getInstance(page);
Run Code Online (Sandbox Code Playgroud)
但是iText7没有PdfImportedPage.
我的用例,我有一页pdf文件.我需要添加一个表并调整pdf内容的大小以适合单个页面.在旧的iText中,我会创建一个页面,添加表格,将现有的pdf页面转换为图像,调整图像大小并将调整后的图像添加到新页面.在iText7中有没有新的方法可以做到这一点.
感谢Bruno的回答,我使用以下代码:
PdfPage origPage = readerDoc.getPage(1);
Rectangle rect = origPage.getPageSize();
Document document = new Document(writerDoc);
Table wrapperTable = new Table(1);
Table containerTable = new Table(new float[]{0.5f,0.5f});
containerTable.setWidthPercent(100);
containerTable.addCell( "col1");
containerTable.addCell("col2");
PdfFormXObject pageCopy = origPage.copyAsFormXObject(writerDoc);
Image image = new Image(pageCopy);
image.setBorder(Border.NO_BORDER);
image.setAutoScale(true);
image.setHeight(rect.getHeight()-250);
wrapperTable.addCell(new Cell().add(containerTable).setBorder(Border.NO_BORDER));
wrapperTable.addCell(new Cell().add(image).setBorder(Border.NO_BORDER));
document.add(wrapperTable);
document.close();
readerDoc.close();
Run Code Online (Sandbox Code Playgroud) 我正在使用iTextPdf构建一个pdf表.每个页面上都有9到15列,确切的数字直到运行时才知道.iTextPDF非常适合在页面宽度上创建大小相同的列.但我无法弄清楚如何创建不同宽度的列.
我不能使用固定的列宽,因为我想跨越整个页面宽度.因此,当写入9列时,每列必须比写入12或15列时更宽.固定的是这些列宽度之间的关系.举个例子,我知道A列总是75%的列宽,它总是列C宽度的50%.我可以为每一列确定这个.
任何人对如何划分页面以正确调整这些列的大小有任何想法?这是我正在使用的一些代码,它们创建了相同大小的列.我最近还需要一些其他东西
cell.setColspan(1);
更改列的宽度,但不更改为固定值.谢谢!
public static void newPDF() throws DocumentException, IOException {
PdfWriter writer;
Document document;
int cols = 9; //can be either 9, 12, or 15
document = new Document();
writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
document.add(createTable(cols));
document.close();
}
public static PdfPTable createTable(int cols) {
PdfPTable table = new PdfPTable(cols);
PdfPCell cell;
for (int i = 0; i < cols; i++) {
ph = selector.process("some text");
cell = new PdfPCell(ph);
cell.setColspan(1); //repeated 9, 12, or 15 times
table.addCell(cell); …Run Code Online (Sandbox Code Playgroud) 我正在开发一个生成Android的应用程序pdf.
我itextpdf用来生成pdf.
我有以下问题:
我有一个有3行的表,当这个表靠近页面的末尾时,有时会在一页上放一行,在下一页放两行.
有没有办法强制这个表在下一页开始,所以我可以在下一页上有完整的表格?
谢谢
itextpdf ×10
itext ×6
java ×6
android ×1
blob ×1
fonts ×1
itextsharp ×1
java-ee ×1
nopcommerce ×1
report ×1