|
@@ -10,14 +10,12 @@ import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.teachcloud.common.enums.PageSizeEnum;
|
|
|
import com.qmth.teachcloud.common.enums.UploadFileEnum;
|
|
|
import com.qmth.teachcloud.common.util.JacksonUtil;
|
|
|
+import com.sun.org.apache.bcel.internal.generic.GOTO;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
@@ -159,10 +157,24 @@ public class PdfUtil {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static PageSizeEnum getPdfFormat(File file) throws IOException {
|
|
|
+
|
|
|
+
|
|
|
Optional.ofNullable(file).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("文件不能为空"));
|
|
|
PdfReader reader = new PdfReader(ByteArray.fromFile(file).value());
|
|
|
- Rectangle pageSize = reader.getPageSize(1);
|
|
|
- return PageSizeEnum.convertToEnum(pageSize.getWidth(), pageSize.getHeight());
|
|
|
+ int totalPages = reader.getNumberOfPages();
|
|
|
+ PageSizeEnum pageSizeEnum = null;
|
|
|
+ for (int i = 1;i < totalPages;i ++){
|
|
|
+ Rectangle pageSize = reader.getPageSize(i);
|
|
|
+ if (pageSizeEnum != null){
|
|
|
+ if (!pageSizeEnum.equals(PageSizeEnum.convertToEnum(pageSize.getWidth(), pageSize.getHeight()))){
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("上传的试卷pdf中存在纸张格式不一样的");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ pageSizeEnum = PageSizeEnum.convertToEnum(pageSize.getWidth(), pageSize.getHeight());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return pageSizeEnum;
|
|
|
}
|
|
|
|
|
|
// public static void main(String[] args) {
|
|
@@ -175,4 +187,77 @@ public class PdfUtil {
|
|
|
// String outputPath = "/Users/king/Downloads/pdf/merge1.pdf";
|
|
|
// PdfUtil.mergePdf(files, outputPath);
|
|
|
// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 两页打印到一页(A4 -> A3)
|
|
|
+ * @param streamOfPDFFile 原卷
|
|
|
+ * @param outputStream 输出的试卷
|
|
|
+ * @param paginate 是否生成A3 每页页码
|
|
|
+ */
|
|
|
+ public static void concatPDFs(InputStream streamOfPDFFile, OutputStream outputStream, boolean paginate) {
|
|
|
+
|
|
|
+ Document document = new Document();
|
|
|
+ try {
|
|
|
+
|
|
|
+ //读取PDF
|
|
|
+ InputStream pdf = streamOfPDFFile;
|
|
|
+ PdfReader pdfReader = new PdfReader(pdf);
|
|
|
+ int totalPages = pdfReader.getNumberOfPages();
|
|
|
+
|
|
|
+ //创建PDF Writer
|
|
|
+ PdfWriter writer = PdfWriter.getInstance(document, outputStream);
|
|
|
+ document.open();
|
|
|
+ BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
|
|
|
+ PdfContentByte contentByte = writer.getDirectContent();
|
|
|
+
|
|
|
+ Rectangle rec = pdfReader.getPageSize(1);
|
|
|
+ //新pdf为两个宽度
|
|
|
+ Rectangle newRec = new Rectangle(0, 0, rec.getWidth() * 2, rec.getHeight());
|
|
|
+ document.setPageSize(newRec);
|
|
|
+
|
|
|
+ PdfImportedPage page;
|
|
|
+ PdfImportedPage page2;
|
|
|
+ int currentPageNumber = 0;
|
|
|
+
|
|
|
+ //舍弃奇数的最后一页, 删除该设置后面不受影响
|
|
|
+// totalPages = totalPages >> 1 << 1;
|
|
|
+ for (int pageIndex = 0; pageIndex < totalPages; pageIndex += 2) {
|
|
|
+ document.newPage();
|
|
|
+ currentPageNumber++;
|
|
|
+
|
|
|
+ //原始第一页设置到左边
|
|
|
+ page = writer.getImportedPage(pdfReader, pageIndex + 1);
|
|
|
+ contentByte.addTemplate(page, 0, 0);
|
|
|
+
|
|
|
+ //第二页设置到右边
|
|
|
+ if (pageIndex + 2 <= totalPages) {
|
|
|
+ page2 = writer.getImportedPage(pdfReader, pageIndex + 2);
|
|
|
+ contentByte.addTemplate(page2, rec.getWidth(), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ //设置页码
|
|
|
+ if (paginate) {
|
|
|
+ contentByte.beginText();
|
|
|
+ contentByte.setFontAndSize(baseFont, 13);
|
|
|
+ contentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + "/" + (totalPages / 2 + (totalPages % 2 == 0 ? 0 : 1)), newRec.getWidth() / 2, 17, 0);
|
|
|
+ contentByte.endText();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ outputStream.flush();
|
|
|
+ document.close();
|
|
|
+ outputStream.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (document.isOpen())
|
|
|
+ document.close();
|
|
|
+ try {
|
|
|
+ if (outputStream != null)
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException ioe) {
|
|
|
+ ioe.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|