HarmonyOS 创建pdf添加文字或者图片,保存到本地打卡是空白内容

pdf相关文档地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/pdf-pdfservice-V5#section699712112399

按照文档描述创建pdf,添加图片和文字,保存到本地Documents目录下后打开是空白的。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

1、添加文字,当前addTextObject,添加文本内容,只可按行添加,只支持单行,不支持换行:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/pdf-arkts-pdfservice-V5#section67811517143913

2、添加文档。

3、创建pdf添加文字或者图片建议参考一下代码(当前图片和pdf文件保存的路径都为沙箱路径,具体可查看:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/share-app-file-V5#应用可分享目录

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { pdfService } from '@kit.PDFKit';
import fs from '@ohos.file.fs';

@Entry
@Component
struct Index {
  @State imageUri: string = ''
  build() {

    Column() {
      Button('选择图片')
        .onClick(() => {
          this.selectImage()
        })
      Image(this.imageUri)
        .width('100%')
        .layoutWeight(1)
      Button('image转PDF')
        .onClick(() => {
          this.savePdf()
        })
    }
  }

  async savePdf() {
    console.info('testTag','enter savePath')
    let pdfDocument = new pdfService.PdfDocument();
    let isCreate = pdfDocument.createDocument(600, 900);
    if (isCreate) {
      console.info('testTag','enter savePath getPageCount = ' + pdfDocument.getPageCount())
      let imagePdfPage = pdfDocument.getPage(0)
      let dir = getContext().filesDir;
      let imgPath = dir + "/test.jpg";
      console.info('testTag','pdf imgPath =' + imgPath)
      imagePdfPage.addImageObject(imgPath, 20, 20, 500, 500);
      let textPdfPage = pdfDocument.insertBlankPage(1, 600, 900)
      let textStyle: pdfService.TextStyle = new pdfService.TextStyle;
      let fontInfo = new pdfService.FontInfo();
      fontInfo.fontPath = "/system/fonts/HarmonyOS_Sans.ttf"
      textStyle.fontInfo = fontInfo;
      textStyle.textSize = 32;
      textStyle.textColor = 234;
      textStyle.isBold = true;
      textStyle.isItalic = false;
      textPdfPage.addTextObject(text, 20, 120, textStyle); //添加示例文字
      let randomNumber = Math.floor((Math.random() * 100));
      let savePath = getContext().filesDir + `/output${randomNumber}.pdf`;
      console.info('testTag','pdf savePath =' + savePath)
      let result = pdfDocument.saveDocument(savePath)
      console.info('testTag','pdf 保存结果:' + result)
    }
  }

  selectImage() {
    let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
    photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    photoSelectOptions.maxSelectNumber = 1
    let photoPicker = new photoAccessHelper.PhotoViewPicker();
    photoPicker.select(photoSelectOptions).then((photo: photoAccessHelper.PhotoSelectResult) => {
      let images = photo.photoUris
      if (images.length > 0) {
        this.imageUri = images[0]
        let file = fs.openSync(images[0], fs.OpenMode.READ_ONLY);
        fs.copyFileSync(file.fd, getContext().filesDir + '/test.jpg');
      }
    }).catch((err: BusinessError) => {
      console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
    });
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
怎么下载网络上PDF保存到本地?
3981浏览 • 1回复 待解决
HarmonyOS 图片保存到相册
19浏览 • 1回复 待解决
HarmonyOS 图片保存到相册报错
292浏览 • 1回复 待解决
如何将Pixmap保存到本地文件?
628浏览 • 1回复 待解决
HarmonyOS 如何将图片保存到相册
129浏览 • 1回复 待解决
HarmonyOS 保存图片本地相册
192浏览 • 1回复 待解决
获取网络图片保存到相册
1802浏览 • 1回复 待解决